In my previous articlewe 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.
📌Types in Kotlin
In Kotlin everything is an object.
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.
📌 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 👏 and share !
Thank you for reading! 🙌🙏😍✌
Top comments (0)