Hey, What About ?
If you read through Kotlin code, you will see variables declared with questions for types:
var something : Boolean? = null
println("something was: $something")
something = true
println("something is now: $something")
Kotlin has many features that are not seen very often in other programming languages. One of the most important of these features is how Kotlin handles null
values. Boolean
is a type that is either true
or false
, while Boolean?
is a type that is either true
, false
, or null
.
We will explore “nullability” in much greater detail later in the book. For now, take it on faith that we can assign null
to a variable whose type ends in ?
, and we cannot assign null
to a variable whose type does not end in ?
. So, where you see ?
in types, watch out for null
values.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.