Nullable Types and the ? Modifier
Declare nullable variables and understand Kotlin's null safety at compile time.
Null Safety in Kotlin
Kotlin distinguishes between types that can hold null and those that cannot — at compile time. The compiler stops most NullPointerExceptions before runtime.
Non-Null Type
By default, a Kotlin type cannot be null. Try to assign null and the compiler refuses.
fun main() {
val name: String = "Ada"
// val nope: String = null // ERROR
println(name)
}All lessons in this course
- Nullable Types and the ? Modifier
- Safe Call ?. and Elvis ?: in Real Code
- let, also, and run with Nullable Receivers
- !! Operator: When and Why to Avoid It