Mobile development

Stage 1 · Lesson 2 of 9

Null safety

Kotlin fundamentals

In a String can never be null; only a String? can. This means the compiler makes you handle the missing case. Learn the tools in order.

First an if (name != null) check, after which Kotlin smart casts, treating the value as a plain String. Then safe calls with ?., the elvis operator ?: for a default, and ?.let { } to run code only when a value exists. Values coming from code have platform types that Kotlin cannot check, so treat them as nullable.

Smart casts do not work on a var property that could change between the check and the use.

Common trap!!, which says "trust me" and throws a NullPointerException when you were wrong.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with· follow one of these

  • GuideFree
    Kotlin Documentation

    JetBrains

    Start with the Kotlin tour, then the null safety and coroutines basics pages

  • CourseFree
    Android Basics with Compose

    Google · Beginner

    The Kotlin pathways in Units 1 to 3 teach the language with practice sets

Practise

  • Dart, TypeScript, Kotlin and Swift tracks with small exercises and free mentor feedback

Keep handy

  • Short Kotlin pages with a try-it editor for checking syntax quickly

Also worth your time

Mark this stage as not done?