Kotlin’s Explicit Null Type
NullPointerExceptions like the one that you saw above should be avoided at all costs. Kotlin protected you by preventing you from assigning a null value to a variable of a non-nullable type. That said, nullness does still exist in Kotlin.
Here is an example, from the header for the function called readLine. readLine accepts user input from the console and returns it so that it can be used later.
public fun readLine(): String?
readLine’s header looks like one that you have seen before, with one exception: the return type String?. The question mark represents a nullable version of a type. That means readLine will either return a value of type String, or it will return null.
Remove your earlier signatureDrink ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access