🐦 Kotlin Beginner

What is the !! operator in Kotlin?

Answer

The non-null assertion operator !! converts a nullable type to a non-nullable type, explicitly telling the compiler "I know this is not null." If the value is actually null at runtime, a KotlinNullPointerException is thrown immediately. Use !! only when you are absolutely certain a value cannot be null — for example, immediately after a null check that the compiler cannot track. In general, avoid !! in production code and prefer ?. or ?:, as !! can introduce the very null crashes Kotlin is designed to prevent.