Kotlin 2 · Basics and null safety
Handle nulls
A type with ? can be null. Use ?. to call safely, ?: for a default, and !! only when you are certain.
val nickname: String? = null
val length = nickname?.length ?: 0
Official Kotlin 2 documentation
Kotlin 2 · Basics and null safety
A type with ? can be null. Use ?. to call safely, ?: for a default, and !! only when you are certain.
val nickname: String? = null
val length = nickname?.length ?: 0
Official Kotlin 2 documentation
Was this page helpful?