Kotlin Basics
Tags: kotlin, androiddev, learnkotlinfromgde, gde
Published: 2020-02-02 16:31:20

---
title: Kotlin Basics
published: true
date: 2019-11-17 21:54:07 UTC
tags: kotlin,androiddev,learnkotlinfromgde,gde
canonical_url: https://medium.com/@magdamiu/kotlin-basics-65470f7e48f4
---

![](https://cdn-images-1.medium.com/max/960/1*DSuVM8kfrgMx8a9Jb6FGeA.png)

In my [previous article](https://dev.to/magdamiu/hello-kotlin-3kof-temp-slug-9275067)we discovered details about how Kotlin was designed, what is the philosophy behind this new programming language and how its popularity has grown over the past years.

In this article we’re going to continue with some basic concepts from Kotlin Wonderland like:

✅basic types from Kotlin

✅control flow instructions

✅equality checks

✅null safety

#### 📌Define variables (val vs _var_)

- **_val_** is immutable (read-only) and we can only assign a value to them exactly one time.
- **_var_** is mutable and can be reassigned.

{% gist https://gist.github.com/magdamiu/ab7cd1d624049cb1a5f3e831773210a3 %}

#### 📌Types in Kotlin

In Kotlin everything is an object.

![](https://cdn-images-1.medium.com/max/960/1*GfldXwnNcYT6Uf8cy3LKXA.png)

{% gist https://gist.github.com/magdamiu/f7f0091fd46eeb377dd64c3fef3711a9 %}

**Arrays** in Kotlin are represented using the Array class. To create an array we can use the helper function _arrayOf()_ or the constructor _Array()_

{% gist https://gist.github.com/magdamiu/71dfa1550d85e88bc142013308a6a494 %}

**String interpolation**

- Simple reference uses _$_
- Complex references uses _${}_
- Raw Strings _”””_

{% gist https://gist.github.com/magdamiu/88897ec240f52defe16aa05386cde953 %}

#### 📌Control flow: if, when, for, while

- **if — ** in Kotlin if is an expression, so it returns a value. There is no ternary operator.
- **when**  — replaces “switch” from Java. We can also check a value for being or not in a specific range and we can also check if a variable is or not of a particular type.
- **for ** — iterates through anything that provides an iterator. Can use the withIndex library function.
- **while** and **do … while**  — same behavior like in Java.

{% gist https://gist.github.com/magdamiu/76bd2f3c30ec9d0eb59758113893b166 %}

{% gist https://gist.github.com/magdamiu/307da58d61f941905172d87b2e66f47b %}

{% gist https://gist.github.com/magdamiu/44d88b01ba261b78c93bedb73bf09051 %}

{% gist https://gist.github.com/magdamiu/2bb093502afd54f96bc4a095d27327bf %}

{% gist https://gist.github.com/magdamiu/b11449f42a8f734bca08307c92c57ec7 %}

{% gist https://gist.github.com/magdamiu/69f58c9faf51a37b28c68a9c8026995f %}

{% gist https://gist.github.com/magdamiu/468f5b136946bd877b5427d32d97fcda %}

#### 📌 **Equality checks**

- In Kotlin we have structural equality (a check for _equals()_) **==**
- Referential equality (two references point to the same object)  **===**

{% gist https://gist.github.com/magdamiu/d35926bb1b5a4f2b1ba7389d5e7fe95f %}

#### 📌Null safety

![](https://cdn-images-1.medium.com/max/850/0*q5iUQslc904rRLl4)

- In an effort to rid the world of _NullPointerException_, variable types in Kotlin don’t allow the assignment of _null_. 👼👼👼
- In order to use a variable that can be _null_, declare it nullable by adding  **?** at the end of its type.

But… 😥😥😥

The only possible causes of NPE’s may be:

- An explicit call to throw _NullPointerException()_
- Usage of the  **!!** operator (not-null assertion operator)
- Some data inconsistency with regard to initialization
- Java inter-operation

{% gist https://gist.github.com/magdamiu/83f12f3f3b8ec23db3a954c5174f8b8e %}

![](https://cdn-images-1.medium.com/max/354/1*zxjeGsDeVvbNIN1fwhZRPg.png)

{% gist https://gist.github.com/magdamiu/bbd3301dc6d23e88f9a7b21ed7a4c16c %}

Enjoy and feel free to leave a comment if something is not clear or if you have questions. And if you like it please 👏 and share !

Thank you for reading! 🙌🙏😍✌

[Magda Miu](https://twitter.com/MagdaMiu)