Null Safety
KOTLIN › Language Idioms
How Kotlin's type system prevents NullPointerExceptions at compile time.
Null safety is one of the most common Kotlin interview topics because it is the language's headline feature and a daily source of bugs on Android. Interviewers test whether you can distinguish nullable from non-null types and pick the right operator (?., ?:, !!, as?) for each situation. They also probe the danger zones: platform types from Java/Android APIs and overuse of !!.
What this covers
- Difference between nullable (String?) and non-null (String) types and how the compiler enforces them
- When to use safe call ?., elvis ?:, non-null assertion !!, and safe cast as?
- How smart casts work after null checks and their limitations (mutable/var properties)
- Platform types (Type!) from Java interop and why they can still cause NPEs
- Using let (and ?.let) to scope logic to the non-null case
- Idiomatic null handling: elvis with return/throw, filterNotNull on collections
Study this topic
- Null Safety explained: the guided lesson
- 13 practice quiz questions
- 9 revision flashcards
- Null Safety interview questions and answers