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

More basics and null safety commands

All basics and null safety commands · Kotlin 2 cheatsheet

Was this page helpful?