Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The !! operator, formally known as the not-null assertion operator, converts a nullable type (T?) to its corresponding non-nullable type (T). It instructs the Kotlin compiler to bypass its standard static null-safety checks, throwing a NullPointerException (NPE) at runtime if the operand evaluates to null. Unlike the as or as? operators, which perform actual type casting, !! is strictly an assertion and compile-time type conversion mechanic.

Syntax and Type Resolution

The operator is applied as a postfix to an expression.
val nullableValue: String? = "Kotlin"
val strictValue: String = nullableValue!!
From a type-system perspective, if e is an expression of type T?, the expression e!! is resolved by the compiler as type T. This type conversion allows the resulting value to be assigned to non-nullable variables, passed as non-nullable arguments, or used as a receiver for properties and functions that strictly reject nulls.

Runtime Evaluation

Unlike Swift’s Optionals or Java’s Optional, Kotlin’s nullable reference types (like String?) are not wrapper objects. At runtime, a nullable reference is the exact same memory reference as a non-nullable one. The !! operator merely performs a null check; there is no “unwrapping” involved (except when unboxing nullable primitives like Int?). When the target runtime executes the !! operator, it performs a strict equality check against null:
  1. Non-null operand: The operator returns the existing memory reference, allowing execution to proceed.
  2. Null operand: The operator immediately halts execution of the current statement and throws a NullPointerException.
var name: String? = null
// The !! operator evaluates 'name'. 
// Because it is null, an NPE is thrown before '.length' is ever accessed.
val length: Int = name!!.length 

Chaining Mechanics

When multiple not-null assertions are chained within a single expression, they are evaluated strictly from left to right.
class Node(val value: String?, val left: Node?)

val node: Node? = Node("Root", Node(null, null))

// Evaluates left-to-right. Throws NPE at .value!! because left.value is null.
val result: String = node!!.left!!.value!!
In this expression:
  • If node is null, the NPE is thrown immediately at node!!.
  • The subsequent property accesses (.left, .value) and their respective assertions are never evaluated due to the immediate exception dispatch.
  • If an NPE occurs, the stack trace will point to the specific line containing the chain, though the exception itself originates directly from the specific !! operator that encountered the null value.

Idiomatic Usage and Alternatives

In Kotlin, using the !! operator is generally discouraged and is widely considered a code smell. Because it intentionally subverts the language’s null-safety guarantees, it introduces the risk of unhandled runtime crashes. Developers should prefer safer, more idiomatic alternatives for handling nullability:
  • Safe call operator (?.): Executes the subsequent call only if the receiver is not null.
  • Elvis operator (?:): Provides a fallback or default value if the expression to its left resolves to null.
  • requireNotNull() or checkNotNull(): Standard library functions that assert non-nullability but throw IllegalArgumentException or IllegalStateException with descriptive, custom error messages instead of a generic NPE.
Master Kotlin with Deep Grasping Methodology!Learn More