Skip to main content
The !== operator is the referential inequality operator in Kotlin. It evaluates to true if two object references point to different memory locations (i.e., they are distinct instances), and false if they point to the exact same instance in memory. It serves as the strict negation of the referential equality operator (===).

Syntax and Translation

The operator is placed between two references:
At compile time, Kotlin translates this expression directly to the negation of a reference check. It does not invoke any methods on the objects.

Referential vs. Structural Inequality

It is critical to distinguish !== from the structural inequality operator (!=).
  • != (Structural Inequality): Evaluates whether the content or state of two objects differs. It is translated to !(a?.equals(b) ?: (b === null)).
  • !== (Referential Inequality): Bypasses the equals() method entirely. It strictly compares the memory addresses of the references on the heap.

Behavior with Primitives and Boxing

In Kotlin 1.6 and later, using identity equality operators (=== and !==) on unboxed primitive types (such as Int, Double, Boolean) results in a strict compilation error. This is because JVM primitives are passed by value and do not possess object identity. However, when these types are boxed (e.g., forced into an object wrapper via nullable types like Int? or generic type parameters), !== evaluates the references of the boxed objects on the heap. This behavior is subject to JVM memory optimizations, such as the Integer cache.

Restrictions with Value Classes

Applying referential equality operators (=== and !==) to value (inline) classes is strictly forbidden in Kotlin and results in a compilation error. Because the Kotlin compiler optimizes value classes by unpredictably boxing or unboxing their underlying data at runtime, they do not possess a guaranteed, stable object identity. Consequently, evaluating memory addresses for these types is semantically invalid.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More