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 evaluates referential equality in Kotlin. It returns true if and only if two references point to the exact same object instance in memory. Its negated counterpart is !==, which returns true if two references point to different memory addresses.
val referenceA = Any()
val referenceB = referenceA
val referenceC = Any()

val isSameReference = referenceA === referenceB      // true
val isDifferentReference = referenceA !== referenceC // true

Mechanical Behavior

The behavior of === depends strictly on how the Kotlin compiler represents the underlying types on the target platform (such as the JVM).

1. Reference Types (Objects)

For standard classes and objects, === compares the memory addresses of the references. It bypasses the equals() method entirely. Even if two distinct objects contain identical data, === will evaluate to false because they occupy different memory allocations.
class Node(val value: Int)

val node1 = Node(10)
val node2 = Node(10)
val node3 = node1

val check1 = node1 === node2 // false: Different memory addresses
val check2 = node1 === node3 // true: Same memory address

2. Primitive Types

Kotlin abstracts primitive types (like Int, Double, Boolean), but at runtime on the JVM, non-nullable representations of these types are compiled down to Java primitives (e.g., int, double). Because JVM primitives do not have memory identities distinct from their values, using the referential equality operator (===) on non-nullable primitive types is deprecated in modern Kotlin. The compiler explicitly warns against this (Identity equality for arguments of types Int and Int is deprecated) because the operation is meaningless and evaluates identically to structural equality.
val x: Int = 1000
val y: Int = 1000

// Deprecated: Identity equality for arguments of types Int and Int is deprecated
val check = x === y 

3. Boxed Types (Nullable Primitives)

When a primitive type is marked as nullable (e.g., Int?), the Kotlin compiler boxes the value into a reference type (e.g., java.lang.Integer). When applying === to boxed types, referential equality is enforced. This can lead to differing results based on JVM memory caching mechanisms (like the Integer cache for values between -128 and 127).
val a: Int? = 1000
val b: Int? = 1000
val c: Int? = 10
val d: Int? = 10

val checkBoxed1 = a === b // false: Different boxed object references (outside cache range)
val checkBoxed2 = c === d // true: Same reference due to JVM Integer caching

Contrast with Structural Equality (==)

To understand === mechanically, it must be contrasted with ==.
  • The == operator evaluates structural equality. It is translated by the compiler to a null-safe invocation of the equals() method: a?.equals(b) ?: (b === null).
  • The === operator evaluates referential equality. It never invokes equals() and strictly compares the pointer/reference value.
Master Kotlin with Deep Grasping Methodology!Learn More