Null Safety
Nullable types, safe casts, let, lateinit and null handling in Kotlin.
Nullable Types in Kotlin
Kotlin types are divided into nullable and non-nullable types. A Kotlin example with source code and sample output.
Safe Cast Operator (as?) in Kotlin
The safe cast operator as? tries to cast a value to another type. A Kotlin example with source code and sample output.
let Function for Null Safety in Kotlin
The let function executes a block only when the object is not null. A Kotlin example with source code and sample output.
also Operator for Null Handling in Kotlin
also performs an action on an object and returns the object itself. A Kotlin example with source code and sample output.
lateinit and Nullable Properties in Kotlin
lateinit is used for non-nullable var properties that are initialized later. A Kotlin example with source code and sample output.
Elvis Operator for Default Values in Kotlin
The Elvis operator ?: returns the left value when it is not null. A Kotlin example with source code and sample output.
Smart Casts in Kotlin
After an is check, the compiler automatically casts the variable to the subtype. A Kotlin example with source code and sample output.