0Pricing
Kotlin Academy · Lesson

Nullable Types and the ? Modifier

Declare nullable variables and understand Kotlin's null safety at compile time.

Null Safety in Kotlin

Kotlin distinguishes between types that can hold null and those that cannot — at compile time. The compiler stops most NullPointerExceptions before runtime.

Non-Null Type

By default, a Kotlin type cannot be null. Try to assign null and the compiler refuses.

fun main() {
    val name: String = "Ada"
    // val nope: String = null // ERROR
    println(name)
}

All lessons in this course

  1. Nullable Types and the ? Modifier
  2. Safe Call ?. and Elvis ?: in Real Code
  3. let, also, and run with Nullable Receivers
  4. !! Operator: When and Why to Avoid It
← Back to Kotlin Academy