Nulls.kt
- 1fun label(name: String?): String {
- 2 val who = name?.trim().orEmpty()
- 3 return if (who.isEmpty()) "reader" else who
- 4}
Nulls.kt
Null is a type
A Kotlin type is non-null unless a question mark says otherwise. String holds text. String? holds text or null. Calling length on a String? does not compile until the null is handled.
The safe call ?. returns null when the receiver is null. The Elvis operator ?: supplies a fallback. A late-initialized Android field is still a promise that someone sets it before the first read.
Java APIs arrive as platform types. A value that looks like String in the editor can still be null at runtime when it crossed from Java. The lesson treats that boundary as part of the type.