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:

✅basic types from Kotlin

✅control flow instructions

✅equality checks

✅null safety

📌Define variables (val vs var)

  • var is mutable and can be reassigned.

📌Types in Kotlin

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

  • 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

  • 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 👏 and share !

Thank you for reading! 🙌🙏😍✌

Squad Lead Developer @orangeromania ✨ Android @GoogleDevExpert ✨ Blogger, Speaker, Trainer #android #kotlin #gde #agile #scrum

Squad Lead Developer @orangeromania ✨ Android @GoogleDevExpert ✨ Blogger, Speaker, Trainer #android #kotlin #gde #agile #scrum