Kotlin Basics

In my previous article 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 the available types, control flow instructions, equality checks and 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.

📌Types in Kotlin

In Kotlin everything is an object, so at the language level we don’t have primitives.

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

String interpolation

  • Simple reference uses $
  • Complex references uses ${}
  • Raw Strings ”””

📌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.


when with range

when with check type


for with index

while and do…while

📌Equality checks

  • In Kotlin we have structural equality (a check for equals()==
  • Referential equality (two references point to the same object) ===

📌Null safety

  • 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

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

Thank you for reading! 🙌🙏😍✌

Follow me on:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s