Module 02 Β· Kotlin, One Level Deeper β Lesson 1 of 5 Β· ~17 min
Nullability: When a Value Might Not Exist
Sometimes there's just no value
Every pet in PawWalk has a name. But its age? A rescue dog's age is often a guess the owner never entered. And not every walker uploads a profile photo. Kotlin forces you to be honest about missing data: a value that might not be there gets a different type.
Double is always a number. Double? β read it as "nullable Double" β is either a number or null, Kotlin's word for "nothing here." In the real models file, Pet.ageYears is a Double? and Walker.photoUrl is a String? for exactly these reasons.
The
?is part of the type.StringandString?are different types, and Kotlin will not let you use aString?as if the value were definitely there. That one rule eliminates the #1 crash in most other languages: using a value that doesn't exist.