Advertisement

Kotlin Val/Var & Null Safety

Previous Lesson 25 of 27 Next
Advertisement
Lesson Overview: Eliminate NullPointerExceptions with nullable types (?) and val immutability.

Safe & Expressive JVM Language

Prefer val (read-only) over var (mutable). Nullable types like String? require safe calls (?.) or Elvis defaults (?:).

  • fun main() { println("Hello") }
  • when replaces verbose switch chains cleanly

Val & Elvis Operator (Editable — type & Run)

Advertisement

Test Yourself With Exercises

Declare an immutable read-only variable in Kotlin:

pi = 3.14159
Advertisement