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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Arrays in Kotlin are represented using the Array class. To create an array we can use the helper function arrayOf() or the constructor Array()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In Kotlin we have structural equality (a check for equals()) ==
Referential equality (two references point to the same object) ===
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.
A simple “thank you” goes a long way—express your gratitude below in the comments!
Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.
Top comments (0)