https://github.com/suhatanriverdi/kotlin-beginners-notes
Kotlin Learning Notes from Udacity Course | Kotlin Bootcamp for Programmers by Google
https://github.com/suhatanriverdi/kotlin-beginners-notes
beginner beginner-code beginner-friendly kotlin kotlin-android kotlin-course kotlin-language learn-to-code
Last synced: about 1 year ago
JSON representation
Kotlin Learning Notes from Udacity Course | Kotlin Bootcamp for Programmers by Google
- Host: GitHub
- URL: https://github.com/suhatanriverdi/kotlin-beginners-notes
- Owner: suhatanriverdi
- Created: 2021-07-25T19:51:59.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2025-01-02T12:25:44.000Z (over 1 year ago)
- Last Synced: 2025-04-02T06:08:39.311Z (about 1 year ago)
- Topics: beginner, beginner-code, beginner-friendly, kotlin, kotlin-android, kotlin-course, kotlin-language, learn-to-code
- Language: Kotlin
- Homepage:
- Size: 468 KB
- Stars: 64
- Watchers: 1
- Forks: 23
- Open Issues: 0
-
Metadata Files:
- Readme: README.html
Awesome Lists containing this project
README
Kotlin Beginners Notes
/* From extension zhuangtongfa.material-theme */
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body {
box-sizing: border-box;
min-width: 200px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body blockquote {
background-color: initial;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body pre {
color: initial;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body code {
color: inherit;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body pre code {
color: initial;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body code > div {
background: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table th, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table td {
border: 1px solid rgba(171, 178, 191, 0.5) !important;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body.showEditorSelection .code-active-line:before {
border-left: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body.showEditorSelection .code-line:hover:before {
border-left: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body.showEditorSelection .code-line .code-line:hover:before {
border-left: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body p, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body blockquote, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ul, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ol, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body dl, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body pre {
margin-top: 16px;
margin-bottom: 16px;
}
/* Generated from 'node_modules/github-markdown-css/github-markdown.css' */
@font-face {
font-family: octicons-link;
src: url(data:font/woff;charset=utf-8;base64,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) format("woff");
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body {
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
line-height: 1.5;
color: #abb2bf;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 16px;
line-height: 1.5;
word-wrap: break-word;
background: #282c34;
padding-top: 20px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-c {
color: #6a737d;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-c1, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-s .pl-v {
color: #005cc5;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-e, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-en {
color: #6f42c1;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-smi, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-s .pl-s1 {
color: #24292e;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-ent {
color: #22863a;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-k {
color: #d73a49;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-s, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-pds, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-s .pl-pse .pl-s1, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-sr, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-sr .pl-cce, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-sr .pl-sre, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-sr .pl-sra {
color: #032f62;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-v, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-smw {
color: #e36209;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-bu {
color: #b31d28;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-ii {
color: #fafbfc;
background-color: #b31d28;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-c2 {
color: #fafbfc;
background-color: #d73a49;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-c2::before {
content: "^M";
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-sr .pl-cce {
font-weight: bold;
color: #22863a;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-ml {
color: #735c0f;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-mh, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-mh .pl-en, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-ms {
font-weight: bold;
color: #005cc5;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-mi {
font-style: italic;
color: #24292e;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-mb {
font-weight: bold;
color: #24292e;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-md {
color: #b31d28;
background-color: #ffeef0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-mi1 {
color: #22863a;
background-color: #f0fff4;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-mc {
color: #e36209;
background-color: #ffebda;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-mi2 {
color: #f6f8fa;
background-color: #005cc5;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-mdr {
font-weight: bold;
color: #6f42c1;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-ba {
color: #586069;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-sg {
color: #959da5;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-corl {
text-decoration: underline;
color: #032f62;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .octicon {
display: inline-block;
vertical-align: text-top;
fill: currentColor;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body a {
background-color: transparent;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body a:active, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body a:hover {
outline-width: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body strong {
font-weight: inherit;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body strong {
font-weight: bolder;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h1 {
font-size: 2em;
margin: 0.67em 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body img {
border-style: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body code, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body kbd, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body pre {
font-family: monospace, monospace;
font-size: 1em;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body hr {
box-sizing: content-box;
height: 0;
overflow: visible;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body input {
font: inherit;
margin: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body input {
overflow: visible;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body [type=checkbox] {
box-sizing: border-box;
padding: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body * {
box-sizing: border-box;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body input {
font-family: inherit;
font-size: inherit;
line-height: inherit;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body a {
color: #528bff;
text-decoration: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body a:hover {
text-decoration: underline;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body strong {
font-weight: 600;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body hr {
height: 0;
margin: 15px 0;
overflow: hidden;
background: transparent;
border: 0;
border-bottom: 1px solid #dfe2e5;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body hr::before {
display: table;
content: "";
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body hr::after {
display: table;
clear: both;
content: "";
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table {
border-spacing: 0;
border-collapse: collapse;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body td, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body th {
padding: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h1, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h2, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h3, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h4, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h5, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h6 {
margin-top: 0;
margin-bottom: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h1 {
font-size: 32px;
font-weight: 600;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h2 {
font-size: 24px;
font-weight: 600;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h3 {
font-size: 20px;
font-weight: 600;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h4 {
font-size: 16px;
font-weight: 600;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h5 {
font-size: 14px;
font-weight: 600;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h6 {
font-size: 12px;
font-weight: 600;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body p {
margin-top: 0;
margin-bottom: 10px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body blockquote {
margin: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ul, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ol {
padding-left: 0;
margin-top: 0;
margin-bottom: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ol ol, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ul ol {
list-style-type: lower-roman;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ul ul ol, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ul ol ol, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ol ul ol, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ol ol ol {
list-style-type: lower-alpha;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body dd {
margin-left: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body code {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 12px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body pre {
margin-top: 0;
margin-bottom: 0;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 12px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .octicon {
vertical-align: text-bottom;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-0 {
padding-left: 0 !important;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-1 {
padding-left: 4px !important;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-2 {
padding-left: 8px !important;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-3 {
padding-left: 16px !important;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-4 {
padding-left: 24px !important;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-5 {
padding-left: 32px !important;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .pl-6 {
padding-left: 40px !important;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body::before {
display: table;
content: "";
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body::after {
display: table;
clear: both;
content: "";
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body > *:first-child {
margin-top: 0 !important;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body > *:last-child {
margin-bottom: 0 !important;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body a:not([href]) {
color: inherit;
text-decoration: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .anchor {
float: left;
padding-right: 4px;
margin-left: -20px;
line-height: 1;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .anchor:focus {
outline: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body p, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body blockquote, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ul, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ol, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body dl, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body pre {
margin-top: 0;
margin-bottom: 16px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body hr {
height: 0.25em;
padding: 0;
margin: 24px 0;
background-color: #e1e4e8;
border: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body blockquote {
/* padding: 0 1em;
color: #6a737d;
border-left: 0.25em solid #dfe2e5; */
padding: 8.5px 17px;
margin: 1.5em 0;
font-size: inherit;
color: #7c879c;
border-color: #4b5362;
border-width: 4px;
border-left: 5px solid #4b5362;
background: transparent;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body blockquote > :first-child {
margin-top: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body blockquote > :last-child {
margin-bottom: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body kbd {
display: inline-block;
padding: 3px 5px;
font-size: 11px;
line-height: 10px;
color: #444d56;
vertical-align: middle;
background-color: #fafbfc;
border: solid 1px #c6cbd1;
border-bottom-color: #959da5;
border-radius: 3px;
box-shadow: inset 0 -1px 0 #959da5;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h1, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h2, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h3, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h4, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h5, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h6 {
margin-top: 24px;
margin-bottom: 16px;
font-weight: 600;
line-height: 1.25;
color: #f0f0f0;
border-bottom: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h1 .octicon-link, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h2 .octicon-link, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h3 .octicon-link, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h4 .octicon-link, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h5 .octicon-link, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h6 .octicon-link {
color: #1b1f23;
vertical-align: middle;
visibility: hidden;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h1:hover .anchor, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h2:hover .anchor, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h3:hover .anchor, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h4:hover .anchor, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h5:hover .anchor, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h6:hover .anchor {
text-decoration: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h1:hover .anchor .octicon-link, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h2:hover .anchor .octicon-link, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h3:hover .anchor .octicon-link, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h4:hover .anchor .octicon-link, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h5:hover .anchor .octicon-link, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h6:hover .anchor .octicon-link {
visibility: visible;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h1 {
padding-bottom: 0.3em;
font-size: 2em;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h2 {
padding-bottom: 0.3em;
font-size: 1.5em;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h3 {
font-size: 1.25em;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h4 {
font-size: 1em;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h5 {
font-size: 0.875em;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body h6 {
font-size: 0.85em;
color: #6a737d;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ul, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ol {
padding-left: 2em;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ul ul, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ul ol, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ol ol, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body ol ul {
margin-top: 0;
margin-bottom: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body li {
word-wrap: break-all;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body li > p {
margin-top: 16px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body li + li {
margin-top: 0.25em;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body dl {
padding: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body dl dt {
padding: 0;
margin-top: 16px;
font-size: 1em;
font-style: italic;
font-weight: 600;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body dl dd {
padding: 0 16px;
margin-bottom: 16px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table {
display: block;
width: 100%;
overflow: auto;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table th {
font-weight: 700;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table th, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table td {
padding: 6px 13px;
/* border: 1px solid #dfe2e5; */
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table tr {
/* background-color: #fff; */
/* border-top: 1px solid #c6cbd1; */
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body table tr:nth-child(2n) {
/* background-color: #f6f8fa; */
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body img {
max-width: 100%;
box-sizing: content-box;
display: inline-block;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body img[align=right] {
padding-left: 20px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body img[align=left] {
padding-right: 20px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body code {
padding: 0.2em 0.4em;
margin: 0;
font-size: 85%;
background-color: #3a3f4b;
border-radius: 3px;
color: white;
margin: 0 1px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body pre {
word-wrap: normal;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body pre > code {
padding: 0;
margin: 0;
font-size: 100%;
word-break: normal;
white-space: pre;
background: transparent;
border: 0;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .highlight {
margin-bottom: 16px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .highlight pre {
margin-bottom: 0;
word-break: normal;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .highlight pre, .vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body pre {
padding: 16px;
overflow: auto;
font-size: 85%;
line-height: 1.45;
/* background-color: #f6f8fa; */
border-radius: 3px;
background-color: #31363f;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body pre code {
display: inline;
max-width: auto;
padding: 0;
margin: 0;
overflow: visible;
line-height: inherit;
word-wrap: normal;
background-color: transparent;
border: 0;
color: #abb2bf;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .full-commit .btn-outline:not(:disabled):hover {
color: #005cc5;
border-color: #005cc5;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body kbd {
display: inline-block;
padding: 3px 5px;
font: 11px "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
line-height: 10px;
color: #444d56;
vertical-align: middle;
background-color: #fafbfc;
border: solid 1px #d1d5da;
border-bottom-color: #c6cbd1;
border-radius: 3px;
box-shadow: inset 0 -1px 0 #c6cbd1;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body :checked + .radio-label {
position: relative;
z-index: 1;
border-color: #0366d6;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .task-list-item {
list-style-type: none;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .task-list-item + .task-list-item {
margin-top: 3px;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body .task-list-item input {
margin: 0 0.2em 0.25em -1.6em;
vertical-align: middle;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"].vscode-body hr {
border-bottom-color: #eee;
}
/*
Atom One Dark by Daniel Gamage
Original One Dark Syntax theme from https://github.com/atom/one-dark-syntax
base: #282c34
mono-1: #abb2bf
mono-2: #818896
mono-3: #5c6370
hue-1: #56b6c2
hue-2: #61aeee
hue-3: #c678dd
hue-4: #98c379
hue-5: #e06c75
hue-5-2: #be5046
hue-6: #d19a66
hue-6-2: #e6c07b
*/
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs {
display: block;
overflow-x: auto;
padding: 0.5em;
color: #abb2bf;
background: #282c34;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-comment,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-quote {
color: #5c6370;
font-style: italic;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-doctag,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-keyword,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-formula {
color: #c678dd;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-section,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-name,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-selector-tag,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-deletion,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-subst {
color: #e06c75;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-literal {
color: #56b6c2;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-string,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-regexp,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-addition,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-attribute,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-meta-string {
color: #98c379;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-built_in,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-class .hljs-title {
color: #e6c07b;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-attr,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-variable,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-template-variable,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-type,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-selector-class,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-selector-attr,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-selector-pseudo,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-number {
color: #d19a66;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-symbol,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-bullet,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-link,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-meta,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-selector-id,
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-title {
color: #61aeee;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-emphasis {
font-style: italic;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-strong {
font-weight: bold;
}
.vscode-dark[data-vscode-theme-name="One Dark Pro"] .hljs-link {
text-decoration: underline;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe WPC', 'Segoe UI', system-ui, 'Ubuntu', 'Droid Sans', sans-serif;
font-size: 14px;
line-height: 1.6;
}
.task-list-item { list-style-type: none; } .task-list-item-checkbox { margin-left: -20px; vertical-align: middle; }
Kotlin Beginners Notes
These are all personal notes taken from the Udacity Course (ud9011) of Kotlin Bootcamp for Programmers by Google as well as other resources. You can use it to learn Kotlin if you are a beginner, I have taken most of the things mentioned in the all sections/videos of this course including some brief pieces from official documentation and official video of JetBrains as well. I also cared about the order of the topics, so it starts from the basics from top to bottom goes to the more advanced ones. This is not an official documentation. You will be probably finding more personal notes and human mistakes :)

Table of Contents
- Lesson 1 & 2 Introduction | Kotlin Basics
- Lesson 3 | Functions
- Lesson 4 | Classes
- Lesson 5 | Kotlin Essentials: Beyond The Basics
Lesson 1 & 2 Introduction | Kotlin Basics
Package Definition and Imports
// Package specification should be at the top of the source file.
package my.demo
import kotlin.text.*
// Main function
fun main(args: Array<String>) {
printHello ()
}
// Alternative main, no need to write parameters!
fun main() {
printHello ()
}
// Prints "Hello Kotlin", println() puts newline, print()
fun printHello () {
println("Hello Kotlin!")
}
// A function that returns "OK" string
fun start(): String = "OK"
// Function returns "Genesis", no need to write "return"
fun returnString () : String = "Genesis"
// Function returns "Genesis2" Alternative Way
fun returnString2 () : String {
return "Genesis2"
}
Operators, -, +, /, *
// Returns integer
println(1 + 1) // Prints: 2
println(53 - 3) // Prints: 50
println(50 / 10) // Prints: 5
println(1 / 2) // Prints: 0
println(6 * 50) // Prints: 300
// Returns double
println(1.0 / 2.0) // Prints: 0.5
println(1.0 / 2) // Prints: 0.5
println(2 / 2.0) // Prints: 1.0
// Kotlin let's you overwrite the basic operators
// You can call methods on variables
val fish = 2
println(fish.times(6)) // Prints: 12
println(fish.div(10.0)) // Prints: 0.2
println(fish.plus(3)) // Prints: 5
println(fish.minus(3)) // Prints: -1
// You can use numbers(basic types) as if they were objects
// Use primitive 'int' as an object
1.toLong() // 1
println(false.not()) // true
Boxing
// Boxing describes the process of converting a primitive value to an object and unboxing therefore the inverse
// All numerical types in Kotlin have a supertype called Number
// Store value one in a variable of type Number
// It'll need to be placed in an object wrapper
// This is called boxing
val boxed: Number = 1
^ ^ ^
name type value
val num: Int = 2
val dob: Double = 2.0
// Both lines do the exact same thing internally
Integer x = 42;
Integer y = Integer.valueOf(42);
// Eventhough this is very handy, it unfortunately leads to a decrease in performance
// We can avoid creating these objets wrappers by not storing numbers in objects
// There are two types of variables in Kotlin
// Changeable & Unchangeable
// var val
// With "val" you can assign value only once
val aquarium = 1
aquarium = 2 // -> ERROR! cannot be reassigned
// You can assign vals;
val str = "string"
val numInt = 1
val numDouble = 1.0
val bool = false
// With "var" you can assign a value, and then you can change it
var fish = 2
fish = 50
// Type is inferred meaning that compiler can figure out the type from the context
// Even so the type is inferred, it becomes fixed at compile time,
// So you cannot change a type of a varible in kotlin once it's type has been determined.
fish = "Bubbles" // ERROR
// We can use variables in operations and there is no punctuation at the end
var str = 8
var a = 5
a + str
print(a + str)
// Number types won't implicitly convert to other types, so you can't assign
// A short value to a long variable or a byte to an int
val b: Byte = 1
val i: Int = b // ERROR Type Mismatch
// But you can always assign them by casting like this;
val i: Int = b.toInt()
// Kotlin supports underscores in numbers
val oneMillion = 1_000_000
val socialSecurityNumber = 999_99_9999L
val hexBytes = 0xFF_EC_DE_5E
val bytes = 0b11010010_01101001_10010100_100100010
// You can speficy long constants in a format that makes sense to you
// The type is inferred by Kotlin
Nullability
// Kotlin helps avoid null pointer exceptions
// When you declare a variables type expicitly, by default its value cannot be null
var rocks: Int = null
// Use the question mark operator to indicate that a variable can be null
var rocks: Int? = null
// Whe you have complex data types such as a list,
var lotsOfFish: List<String?> = listOf(null, null)
// You can allow for the list to be null, but if it is not null its elements cannot be null
var evenMoreFish: List<String>? = null
var definitelyFish: List<String?>? = null
// Or you can allow both the list or the elements to be null
definitelyFish = listOf(null, null)
// Samples
// Creating List
var names5: List<String> = listOf("asd", "adsad3")
// Allow list to be null
var names: List<String>? = null
// Allow list items to be null
// But list cannot be null
var names2: List<String?> = listOf()
// ERROR, List cannot be null
var names3: List<String?> = null
// Allow both list items and list itself to be null
// But list cannot be null
var names4: List<String?>? = null
var a: String = "abc" // Regular initialization means non-null by default
a = null // compilation error
// it's guaranteed not to cause an NPE, so you can safely say:
val l = a.length
// To allow nulls, you can declare a variable as nullable string, written String?:
var b: String? = "abc" // can be set null
b = null // ok
val l = b.length // error: variable 'b' can be null
print(b)
// List with some null items
var listWithNulls: List<String?> = listOf("Kot", null, "melo", null)
// Checking for null in conditions
// Option 1: First, you can explicitly check if b is null, and handle the two options separately:
val l = if (b != null) b.length else -1
// Option 2: Safe calls, Your second option is the safe call operator, written "?."
val a = "Kotlin"
val b: String? = null
println(b?.length)
println(a?.length) // Unnecessary safe call
// This returns "b.length" if "b is not null", and "null" otherwise. The type of this expression is Int?
// "b?.length" is equal to "if (b != null) b.length else -1"
var b: String? = "abc"
val l = if (b != null) b.length else -1
val l2 = b?.length
// Such a chain returns null if any of the properties in it is null.
bob?.department?.head?.name
// If you want to do an operation on the non-null items
// To perform a certain operation only for non-null values, you can use the safe call operator together with let:
val listWithNulls: List<String?> = listOf("Kotlin", null)
for (item in listWithNulls) {
item?.let { println(it) } // prints Kotlin and ignores null
}
// Print also nulls
var listWithNulls: List<String?> = listOf("Genesis", null, "melo", null)
print(listWithNulls)
Elvis operator " ?: "
/*If the expression to the left of ?: is not null, the elvis operator returns it, otherwise it returns the expression to the right. Note that the right-hand side expression is evaluated only if the left-hand side is null.*/
val l: Int = if (b != null) b.length else -1
val l = b?.length ?: -1
/*If the expression to the left of ?: is not null, the elvis operator returns it, otherwise it returns the expression to the right. Note that the right-hand side expression is evaluated only if the left-hand side is null.*/
var b: String? = null
var l = b?.length ?: -1
print(l)
/*Since throw and return are expressions in Kotlin, they can also be used on the right hand side of the elvis operator. This can be very handy, for example, for checking function arguments:
*/
fun foo(node: Node): String? {
val parent = node.getParent() ?: return null
val name = node.getName() ?: throw IllegalArgumentException("name expected")
// ...
}
The " ! " Operator & Not-null Assertion Operator " !! "
/*This is unsafe nullable type (T?) conversion to a non-nullable type (T), !// will throw NullPointerException if the value is null.*/
/*The third option is for NPE-lovers: the not-null assertion operator (!!) converts any value to a non-null type and throws an exception if the value is null. You can write b!!, and this will return a non-null value of b (for example, a String in our example) or throw an NPE if b is null:*/
val l = b!!.length
/*Thus, if you want an NPE, you can have it, but you have to ask for it explicitly, and it does not appear out of the blue.*/
Safe casts
/*Regular casts may result into a ClassCastException if the object is not of the target type. Another option is to use safe casts that return null if the attempt was not successful:*/
// Safe Casts
var a = "1"
var b = 5
var aInt: Int? = a as? Int
var bInt: Int? = b as? Int
print(aInt) // null
print(bInt) // 5
Collections of a nullable type
/*If you have a collection of elements of a nullable type and want to filter non-null elements, you can do so by using filterNotNull:*/
val nullableList: List<Int?> = listOf(1, 2, null, 4)
val intList: List<Int> = nullableList.filterNotNull()
var nullableList: List<Int?> = listOf(1, null, 2, null, null, 5)
var nonNullList = nullableList.filterNotNull()
print(nonNullList) // Prints: [1, 2, 5]
// You can do some cool null testing with the question mark operator saving you the pain of many if else statements
// You can check if an object or variable is non null before accessing one of its methods
val fishFoodTreats: Int? = null
return fishFoodTreats?.dec() ?: 0
val fishFoodTreats = 5
return fishFoodTreats?.dec() ?: 0
// You can also chain null tests in an expression
/*If "fishFoodTreats" is not null use a treat and return a new value and otherwise return the value after the colon which is zero*/
Practice Time: Basic Operations
/*
Solve the following using the operator methods in one line of code.
If you start with 2 fish, and they breed twice, producing 71 offspring the first time, and 233 offspring the second time, and then 13 fish are swallowed by a hungry moray eel, how many fish do you have left? How many aquariums do you need if you can put 30 fish per aquarium?
Hint: You can chain method calls.
Hint: You can call the methods on numbers, and Kotlin will convert them to objects for you.
Bonus question: What is special about all the numbers of fish?*/
// Solution Code
2.plus(71).plus(233).minus(13).div(30).plus(1)
// Bonus question: If you've noticed, all fish numbers above are prime.
// My One Line Solution
println(((2.times(71).plus(2.times(233))).minus(13)).div(30).plus((if (595.mod(30) > 0) 1 else 0)))
// To find how many fishes left
println((2.times(71).plus(2.times(233))).minus(13))
// To find Aquariums Needed
println(595.div(30) + if (595.mod(30) > 0) 1 else 0)
Practice Time: Variables
/*Create a String variable rainbowColor, set its color value, then change it.
Create a variable blackColor whose value cannot be changed once assigned. Try changing it anyway.*/
var rainbowColor: String = "green"
rainbowColor = "blue"
val blackColor: String = "you cannot change me// I am pure Black!"
blackColor = "White!"
//Alternative
var rainbowColor = "green"
rainbowColor = "blue"
val blackColor = "black"
blackColor = "white" // Error
Practice Time: Nullability
// Try to set rainbowColor to null. Declare two variables, greenColor and blueColor. Use two different ways of setting them to null.
var rainbowColor = "red"
rainbowColor = null // Error
var greenColor = null
var blueColor: Int? = null
Practice Time: Nullability/Lists
// Create a list with two elements that are null; do it in two different ways.
// Next, create a list where the list is null.
// list with two null items
var list = listOf(null,null)
var list1: List<Int?> = listOf(null, null)
// The list2 itself is null
var list2: List<Int>? = null
Practice Time: Null Checks
// Create a nullable integer variable called nullTest, and set it to null. Use a null-check that increases the value by one if it's not null, otherwise returns 0, and prints the result.
// Hint: Use the Elvis operator.
var nullable: Int? = null
println(nullable?.inc() ?: 0)
println(nullTest?.inc() ?:0)
Strings
"Hello Fish" // Hello Fish
// Concatenation
"hello" + "fish" // hello fish
val numberOfFish = 5
val numberOfPlants = 12
"I have $numberOfFish fish and $numberOfPlants plants" // I have 5 fish and 12 plants
// Here two numbers get added first then the result will be printed
"I have ${numberOfFish + numberOfPlants} fish and plants" // I have 17 fish and plants
val fish = "fish"
val plant = "plant"
println(fish == plant) // false
println(fish != plant) // true
val A = "A"
val B = "Z"
println(A < B) // true
println(A > B) // false
If-Else Blocks
val numberOfFish = 50
val numberOfPlants = 23
if (numberOfFish > numberOfPlants) {
println("Good Ratio!")
} else {
println("unhealthy ratio")
}
Ranges
val fish = 50
// .. -> inclusively 1 <= fish <= 50
if (fish in 1..50) {
println(fish.toString() + " is in the range 1 <= fish <= 50!")
}
// until -> exclusively 1 <= fish < 50
if (fish in 1 until 50) {
println(fish)
} else {
println(fish.toString() + " is not in the range 1 <= fish < 50!")
}
When
// "when" is the way of switching in Kotlin
val numberOfFish = 50
when (numberOfFish) {
0 -> println("Empty tank")
50 -> println("Full tank")
else -> println("Perfect!")
} // Output: Full tank
val numberOfFish = 50
when (numberOfFish) {
in 1..50 -> println("Full tank")
} // Output: Full tank
// Create a string which would contain a * symbol n times.
val str: String = "*".repeat(100)
Practice Time
// Create three String variables for trout, haddock, and snapper.
// Use a String template to print whether you do or don't like to eat these kinds of fish.
var trout: String = "trout"
var haddock: String = "haddock"
var snappe: String = "snappe"
var currentFish = trout
when (currentFish) {
"trout" -> println("I love it!")
"haddock" -> println("I like it")
"snappe" -> println("I hate it")
else -> println("That's enough fish")
}
val trout1 = "trout"
var haddock1 = "haddock"
var snapper1 = "snapper"
println("I like to eat $trout1 and $snapper1, but not a big fan of $haddock1.")
Practice Time
/*when statements in Kotlin are like case or switch statements in other languages.
Create a when statement with three comparisons:
If the length of the fishName is 0, print an error message.
If the length is in the range of 3...12, print "Good fish name".
If it's anything else, print "OK fish name".*/
var fishName = "Salmon"
when (fishName.length) {
0 -> println("Fish name cannot be empty!")
in 3..12 -> println("Good fish name")
else -> println("OK fish name")
}
Arrays & Loops
// If val variable value is a reference, then you cannot assign it a different reference later
val myList = mutableListOf("tuna", "salmon", "shark");
myList = mutableListOf("Koi"); // ERROR// Cannot be re-assigned
// If you're referencing something that's not immutable(değişmez), it can still change
// val only applies to the reference and it doesn't make the object it points to immutable
// Here we cannot assign a different list in myList but we can manipulate the elemets of the list such as removing/adding an element
val myList = mutableListOf("tuna", "salmon", "shark");
myList.remove("shark") // True
myList.add("fish") // True
For/While Loop Examples
val myList = mutableListOf("tuna", "salmon", "shark");
// Loop through an array
for (item in myList) {
print(item + " ") // tuna salmon shark
}
// Loop through an array With index
for (index in myList.indices) {
print(myList[index] + " ") // tuna salmon shark
}
for ((index, value) in myList.withIndex()) {
println("the element at $index is $value") // the element at 0 is tuna...
}
// for (i in array.indices) {
// println(array[i])
// }
// To iterate over a range of numbers, use a range expression
for (i in 1..5) {
// print(i.toString() + " ") /// Alternate
print("$i ") // 1 2 3 4 5
}
for (c in 'a'..'z') {
print("$c ") // a b c d e f g h i j k l m n o p q r s t u v w x y z
}
for (c in 'z' downTo 'a') {
print("$c ") // z y x w v u t s r q p o n m l k j i h g f e d c b a
}
for (c in 10 downTo 0) {
print("$c ") // 10 9 8 7 6 5 4 3 2 1 0
}
for (c in 10 downTo 0 step 2) {
print("$c ") // 10 8 6 4 2 0
}
for (c in 1..10 step 2) {
print("$c ") // 1 3 5 7 9
}
While loop
var x = 5
while (x > 0) {
print("$x ") // 5 4 3 2 1
x--
}
// Arrays work pretty much as you'd expect with some cool additions
// Good practice is to prefer using "lists" over "arrays" everywhere except for performance critical parts of your code
// It is pretty similar to Java
val l1 = listOf("a")
val l2 = listOf("a")
var x = (l1 == l2) // => true
val a1 = arrayOf("a")
val a2 = arrayOf("a")
var y = (a1 == a2) // => false
listOf vs mutableListOf
/*
List: READ-ONLY
MutableList: READ/WRITE
You can modify a MutableList: change, remove, add... its elements.
In a List you can only read them.
// Prefer MutableList over Array
// The major difference from usage side is that;
-> Arrays have a fixed size (like int [] in C++)
-> MutableList can adjust their size dynamically (like vectors in C++, a.k.a dynamic arrays)
-> Moreover Array is MUTABLE whereas List is not. (List is read-only, Array is not)
// Difference between ArrayList<String>() and mutableListOf<String>() in Kotlin
-> The only difference between the two is communicating your intent :)
-> So, there is no difference, just a convenience method.*/
// Create an array
val school = arrayOf("fish", "tuna", "salmon")
// Create Typed array (e.g. integers)
val numbers = intArrayOf(1, 2, 3)
// Error, Type Mismatch
val test = intArrayOf(2, "foo")
// But you can mix types in Untyped arrays
val mixedArray = arrayOf("fish", 2, 's', 0.0)
for (element in mixedArray) {
println(element) // fish 2 s 0.0
// print(element.toString() + " ")
}
// This does not prints the all elements, it prints the array address instead
val mixedArray = arrayOf("fish", 2, 's', 0.0)
print(mixedArray) // [Ljava.lang.Object;@66d3c617
// You can use joinToString or forEach, forEachIndexed, Arrays.toString( array )
val mixedArray = arrayOf("fish", 2, 's', 0.0)
print(mixedArray.joinToString()) // fish, 2, s, 0.0
mixedArray.forEach { print("$it ") } // fish, 2, s, 0.0
mixedArray.forEachIndexed { index, any -> println("$any at $index") }
// fish at 0 ...
println(Arrays.toString(mixedArray)) // [fish, 2, s, 0.0]
Nesting Arrays
// You can nest arrays
val swarm = listOf(5, 12)
// When you put an array within an array, you have an array of arrays
// !Not a flattened array of the contents of the two
val bigSwarm = arrayOf(swarm, arrayOf("A", "B", "C"))
println(Arrays.toString(bigSwarm))
println(bigSwarm.asList()) // Shorter Printing Array Alternative
// Prints: [[5, 12], [Ljava.lang.String;@452b3a41]
// You can nest arrays
val intList = listOf(5, 12)
val stringList = mutableListOf("A", "B", "C")
// OR this -> val stringList = listOf("A", "B", "C")
// When you put "LİST or MUTABLELIST" within an array, you have an array of arrays with merged content but "ARRAYS" are passed by ref as shown above example
val bigList = listOf(intList, stringList)
println(bigList.joinToString())
// [5, 12], [A, B, C]
Create Typed Lists, Mutablelists and Arrays
val intList = listOf<Int>(5, 12)
val stringList = listOf<String>("1","2","3","4")
// Mutablelists
val intList = mutableListOf<Int>(5, 12)
val stringList = mutableListOf<String>("1","2","3","4")
// Array
val intList = arrayOf<Int>(5, 12)
val stringList = arrayOf<String>("1","2","3","4")
// Sized array
var table = Array<String>(words.size) {""}
val literals = arrayOf<String>("January", "February", "March")
// Create 2D Array
val grid = Array(rows) { Array(cols) { Any() } }
//String[] in Java equivalent Array<String> in Kotlin
//eg.
var array1 : Array<String?> = emptyArray()
var array2: Array<String?> = arrayOfNulls(4)
var array3: Array<String> = arrayOf("Mashroom", "Kitkat", "Oreo", "Lolipop")
val num = arrayOf(1, 2, 3, 4) //implicit type declaration
val num = arrayOf<Int>(1, 2, 3) //explicit type declaration
// Or you can also create typed lists, arrays, mutable lists
val intList = listOf<Int>(5, 12)
val listSample: List<Int> = listOf(1,2,3)
val mutableListSample: MutableList<Int> = mutableListOf(1,2,3)
val stringList = listOf<String>("1","2","3","4")
val stringListSample: List<String> = listOf<String>("1","2","3","4")
var initList = List(4){"s"} // {"s", "s", "s", "s"}
// Arrays
val intArray = arrayOf(1,2,3)
val intArray2: Array<Int> = arrayOf(1,2,3)
val intArray3 = intArrayOf(1,2,3)
val charArray = charArrayOf('a', 'b', 'c')
val intArray = arrayOf(1,2,3)
val intArray2: Array<Int> = arrayOf(1,2,3)
val intArray3 = intArrayOf(1,2,3)
val charArray = charArrayOf('a', 'b', 'c')
val stringArray = arrayOf("genesis", "melo")
val stringArray2: Array<String> = arrayOf("genesis", "melo")
val stringOrNulls = arrayOfNulls<String>(5) // returns Array<String?>
val stringOrNulls2: Array<String?> = arrayOf("", null)// returns Array<String?>
var emptyStringArray: Array<String> = emptyArray()
var emptyStringArray2: Array<String> = arrayOf()
var sizedEmptyArray = Array(4){"s"} // {"s", "s", "s", "s"}
// In this line, we create an array from a range of numbers.
val nums3 = IntArray(5, { i -> i * 2 + 3})
// This line creates an array with IntArray. It takes the number of elements and a factory function as parameters.
// This is the output.
/*
[1, 2, 3, 4, 5]
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[3, 5, 7, 9, 11]
*/
// You can read this as initialize an array of 5 elements, assign each item to its index times two
val array = Array(5) {it * 2}
// OR -> val array = List(5) {it * 2}
println(array.joinToString()) // 0, 2, 4, 6, 8
val list = List(5){ it.times(2) } // Creates List: [0, 2, 4, 6, 8]
val array = Array<Int>(5) { e -> if (e % 2 == 0) 2 else 1 }
println(array.asList())
val array = List<String>(5) { e -> if (e % 2 == 0) "even" else "$e" }
println(array.joinToString()) // Prints: even, 1, even, 3, even
// Loop array with indices
val swarm = listOf(5, 12, 15, 17)
for (i in 0 until swarm.size) {
print("$i ") // 0 1 2 3
}
for (i in 0..swarm.size - 1) {
print("$i ") // 0 1 2 3
}
for (i in swarm.indices) {
print("$i ") // 0 1 2 3
}
for (indexValuePair in swarm.withIndex()) {
print("index: ${indexValuePair.index}, value: ${indexValuePair.value}\n")
} // Prints: index: 0, value: 5
Quiz
// Read the code below, and then select the correct array initialization that will display the corresponding output.
val array = // initalize array here
val sizes = arrayOf("byte", "kilobyte", "megabyte", "gigabyte",
"terabyte", "petabyte", "exabyte")
for ((i, value) in array.withIndex()) {
println("1 ${sizes[i]} = ${value.toLong()} bytes")
}
// Output:
1 byte = 1 bytes
1 kilobyte = 1000 bytes
1 megabyte = 1000000 bytes
1 gigabyte = 1000000000 bytes
1 terabyte = 1000000000000 bytes
1 petabyte = 1000000000000000 bytes
1 exabyte = 1000000000000000000 bytes
// Answer / Solution Code:
val array = Array(7){ 1000.0.pow(it) }
// Notice how we had to use the double value 1000.0 and not just 1000 to be able to use the "pow" function.
Quiz
/* Which of these options are good reasons to explicitly make a list immutable? There may be more than 1 correct answer.
-> It reduces errors in general.
-> Prevents accidental changing of objects that were meant to be unchangeable.
-> In a multi-threaded environment, makes the variable thread safe, because once it has been assigned by the initial thread, no thread can change it.*/
// Answer: Immutable variables are the safest option when you know that a variable will never need to change values.
Practice Time
/*Looping over arrays and lists is a fundamental technique that has a lot of flexibility in Kotlin. Let's practice.
Basic example
Create an integer array of numbers called numbers, from 11 to 15.
Create an empty mutable list for Strings.
Write a for loop that loops over the array and adds the string representation of each number to the list.
Challenge example
How can you use a for loop to create (a list of) the numbers between 0 and 100 that are divisible by 7?*/
// Solution Code
var list3 : MutableList<Int> = mutableListOf()
for (i in 0..100 step 7) list3.add(i)
print(list3)
[0, 7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]
// OR
for (i in 0..100 step 7) println(i.toString() + " - ")
// My Solution
val numbers = Array<Int>(5) { it + 11 }
println(numbers.asList())
var mutableList = mutableListOf<String>()
for (number in numbers) {
mutableList.add(number.toString())
}
println(mutableList)
// Challange Example
for (number in 7..100 step 7) {
println("$number ")
}
Kotlin Lists (from Jetbrains Official Video)
listOf(1, 2, 3)
// [1, 2, 3]
val l2 = List(5){ "No. $it" }
val l3 = List(5){ idx -> "No. $idx" }
// [No. 0, No. 1, No. 2, No. 3, No. 4]
val l4 = "word-salad".toList()
// [w, o, r, d, -, s, a, l, a, d]
val m1 = mapOf(
1 to "Gold",
2 to "Silver",
3 to "Bronze"
).toList()
// [(1, Gold), (2, Silver), (3, Bronze)]
generateSequence {
Random.nextInt(100).takeIf { it > 30 }
}.toList()
// [45, 75, 74, 31, 54, 36, 63]
(0..5).toList()
// [0, 1, 2, 3, 4, 5]
val mutList = mutableListOf(1, 2, 3)
val otherList = mutList.toList()
mutList[0] = 5
mutList
// [5, 2, 3]
otherList
// [1, 2, 3]
val myList = listOf("A", "B", "C")
myList.get(0)
myList[0]
myList.getOrNull(3)
val test = myList.getOrElse(3) {
println("There is no index $it")
":("
}
// There is no index 3
// :(
val listOfNullableItems = listOf(1, 2, null, 4)
// Elvis operator checks if item at index 2 null, if it is null it returns 0,
// returns the element at index 2 otherwise
val item = listOfNullableItems[2] ?: 0
val myList1 = listOf("a", "b", "c", "d", "e")
myList1.slice(listOf(0, 2, 4))
// [a, c, e]
myList1.slice(0..3)
// [a, b, c, d]
myList1.slice(0..myList1.lastIndex step 2)
// [a, c, e]
myList1.slice(2 downTo 0)
// [c, b, a]
mutableListOf(1, 2, 3)
// [1, 2, 3]
(0..5).toMutableList()
// [0, 1, 2, 3, 4, 5]
listOf(1, 2, 3).toMutableList()
// [1, 2, 3]
val m = mutableListOf(1, 2, 3)
m.add(4)
m += 4
// [1, 2, 3, 4, 4]
m.add(2, 10)
// [1, 2, 10, 3, 4, 4]
// Append a list to another list
m += listOf(5, 6, 7)
// [1, 2, 10, 3, 4, 4, 5, 6, 7]
val mList = mutableListOf(1, 2, 3, 3, 3, 4)
mList -= 3
mList.remove(3)
// [1, 2, 3, 4]
// Removes all instances of given elements from the mList
mList -= listOf(1, 4)
// [2, 3]
mList.removeAt(1)
// [2]
mList[0] = 5
// [5]
val fruits = mutableListOf("Apple", "Apricot", "Cherry")
fruits.fill("sugar")
// [sugar, sugar, sugar]
fruits.clear()
// []