← Back to Kotlin Course | Chapter 5: Null Safety | Lesson 1 of 6

Nullable Types

A nullable type is Kotlin's way of labeling a box that is allowed to be completely empty, so the compiler can warn you before you peek inside carelessly.

Nullable vs Non-Nullable Types

Appending a ? to a type, such as String?, marks it as nullable, meaning a variable of that type may hold either a real value or null. A plain String (without ?) can never be null.

Example: Nullable vs Non-Nullable Types

markup
fun main() {
    var name: String = "Kotlin"
    var nickname: String? = null
    println("Name: $name, Nickname: $nickname")
}

The Compiler Prevents Null Pointer Exceptions

Because the type system tracks nullability, the compiler refuses to compile code that could crash by calling a method on a null value, forcing you to handle that possibility explicitly.

Example: The Compiler Prevents Null Pointer Exceptions

markup
fun printLength(text: String?) {
    if (text != null) {
        println("Length: ${text.length}")
    } else {
        println("Text is null")
    }
}

fun main() {
    printLength("Kotlin")
    printLength(null)
}

Smart Casting a Nullable Value

After a null check like if (text != null), Kotlin smart-casts text to its non-nullable type inside that block, so it can be used just like a normal String without any extra unwrapping.

Example: Smart Casting a Nullable Value

markup
fun shout(text: String?) {
    if (text != null) {
        println(text.toUpperCase())
    }
}

fun main() {
    shout("hello")
    shout(null)
}

Function Parameters as Nullable

Marking a function parameter as nullable communicates clearly to callers that passing null is an expected, valid case that the function itself will handle.

Example: Function Parameters as Nullable

markup
fun describeAge(age: Int?): String {
    return if (age == null) "Age unknown" else "Age is $age"
}

fun main() {
    println(describeAge(30))
    println(describeAge(null))
}
Common Mistakes
  1. Forgetting the ? after a type when a value can legitimately be absent, causing the compiler to disallow assigning null to it.
  2. Treating a nullable type like a regular one and calling a method directly, which the compiler rejects until the null case is handled.
  3. Overusing nullable types everywhere out of caution, when many values could be designed to never be null in the first place.
Chapter Summary
  • A type followed by ?, such as String?, can hold either a value of that type or null.
  • A non-nullable type, like plain String, can never hold null; the compiler enforces this at compile time.
  • Calling a member directly on a nullable type without handling the null case is a compile-time error.
  • Kotlin's null safety catches many null pointer exceptions before the program ever runs.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.