Kotlin's type system separates nullable and non-null references—one of the biggest wins over Java Optional patterns and null checks.
Nullable types
var name: String = "Ada"
// name = null // compile error
var nick: String? = null
println(nick?.length) // safe call
println(nick ?: "guest") // Elvis default
Operators
?.— safe call returns null if receiver is null?:— Elvis operator provides default!!— force unwrap (throws NPE—avoid in production)?.let { }— run block only when non-null
Important interview questions and answers
- Q: Why avoid !!?
A: It throws KotlinNullPointerException—defeats the type system; use safe calls or early returns. - Q: Java interop null?
A: Platform types from Java are treated as nullable or not at your risk—annotate Java with @Nullable/@NonNull when possible.
Self-check
- How do you declare a nullable String?
- What does the Elvis operator do?
Pitfall: Avoid !! in production—use safe calls, Elvis, or early returns instead.
Interview prep
- Why avoid !!?
Throws NPE and defeats null safety.
- Java interop null?
Platform types from Java need careful handling.