Module 03 of 12

Null Safety & Types

Lessons

About This Module

Tony Hoare called the null reference his "billion-dollar mistake," and Kotlin's answer is to build null safety into the type system. A regular type like String can never hold null, and a type that can is marked explicitly with a question mark, as in String?. The compiler then refuses to let you use a nullable value unsafely, which removes most NullPointerExceptions before the program ever runs.

This module covers the tools for working with nullable values: the safe call operator (?.), the Elvis operator (?:) for default values, and the non-null assertion (!!) and why to use it sparingly. You'll also learn smart casts, where the compiler tracks type checks for you, and safe casts with as?, so you can handle types confidently.

Watch the lessons in order, then continue on to Functions & Lambdas.

Lessons

4 videos
01

Kotlin for Beginners: Null Safety and Smart Casts

A beginner-friendly introduction to Kotlin's null safety and smart casts, and how they help you write more robust code.

02

Kotlin Null Safe Operators

Covers the safe call operator, using let with nullable values, the Elvis operator, and the non-null assertion operator.

03

The Elvis Operator and Chaining Safecall: Kotlin for Complete Beginners 035

Shows how the Elvis operator returns a fallback value when an expression is null, and how safe calls chain through nullable values.

04

Kotlin Null Safety Best Practices: Avoid Null Bugs with Safe Calls & Elvis Operator

Best practices for handling nullability effectively with safe calls and the Elvis operator, to avoid null bugs in real code.