How does Kotlin handle null safety?
Assesses fundamental understanding of Kotlin conventions, runtime behavior, and memory/performance considerations.
Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.
Kotlin's type system distinguishes nullable from non-nullable types. String cannot hold null; String? can. This moves null checks to compile time and largely eliminates NullPointerException.
var a: String = "hi"
var b: String? = null
// a.length // compile error on b.length
println(b?.length) // safe call, prints null
println(b?.length ?: 0) // Elvis operator, default value
b?.let { println(it) } // runs only when non-null
val c = b!!.length // asserts non-null, throws if null
Tools include the safe call ?., the Elvis operator ?:, let/also, and the not-null assertion !!, which should be avoided because it reintroduces crashes. lateinit var is for non-null properties initialised after construction, and by lazy for deferred initialisation. Values coming from Java have platform types with unknown nullability, so guard or annotate them at the boundary.
Candidate Response Strategy & Interview Tips
- Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
- Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
- Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
- Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.