Nothing & Any
KOTLIN › Types & Classes
Kotlin's type hierarchy: Any as the root of all non-nullable types, Nothing as the bottom type with zero instances.
Understanding Nothing and Any reveals how Kotlin's type system actually works under the hood. Interviewers use these to separate candidates who can reason about types from those who just memorise syntax. Expect questions on why throw is an expression of type Nothing, how emptyList() is backed by a List<Nothing> singleton that assigns to any List<T>, what Nothing? means, and where Any sits relative to Java's Object. These come up in generics, error handling, and sealed-hierarchy design.
What this covers
- Any is the root of every non-nullable type; Any? is the true top of the entire type hierarchy
- Nothing is the bottom type: a subtype of every type, with zero possible instances
- Functions that never return (throw, TODO(), infinite loops) have return type Nothing
- throw is an expression typed Nothing, which is why it works inside when branches and elvis
- Nothing? has exactly one value: null; it is the type the compiler infers for standalone null
- emptyList() is backed by a List<Nothing> singleton; covariance + Nothing <: T makes it assignable to any List<T>