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 in Kotlin evaluates structural inequality. It determines whether two object references contain different underlying data (values), abstracting away the complexity of manual null-checking and method invocation.

Compiler Desugaring and Null Safety

Unlike Java, where != on objects evaluates memory addresses, Kotlin’s != operator is translated by the compiler into a negated, null-safe call to the equals() function. When you write a != b, the Kotlin compiler desugars it to the following expression:
!(a?.equals(b) ?: (b === null))
Execution Flow:
  1. Safe Call (?.): The compiler first checks if the left operand (a) is null.
  2. Non-Null Left Operand: If a is not null, it invokes a.equals(b).
  3. Null Left Operand: If a is null, the safe call returns null, triggering the Elvis operator (?:). The expression then evaluates b === null (referential equality) to check if the right operand is also null.
  4. Logical Negation (!): The final boolean result of the equality check is inverted.

Customizing Behavior

Because != is tied to structural equality, you cannot overload the != operator directly using an operator fun. Instead, its behavior is dictated by the implementation of the equals(other: Any?): Boolean function inherited from Any. To define custom structural inequality for a class, you must override equals():
class Entity(val id: String) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other !is Entity) return false
        return this.id == other.id
    }

    // hashCode must always be overridden when equals is overridden
    override fun hashCode(): Int = id.hashCode()
}

// The != operator now implicitly utilizes the overridden equals() logic
val isDifferent = Entity("1") != Entity("2") 

Contrast with Referential Inequality (!==)

It is critical to distinguish the structural inequality operator (!=) from the referential inequality operator (!==).
  • != evaluates state/value differences via the equals() contract.
  • !== evaluates memory address differences, returning true only if the two references point to completely different objects in the heap, regardless of their internal state.
val str1 = String(charArrayOf('k', 'o', 't', 'l', 'i', 'n'))
val str2 = String(charArrayOf('k', 'o', 't', 'l', 'i', 'n'))

val structural = (str1 != str2)   // Evaluates to false (values are identical)
val referential = (str1 !== str2) // Evaluates to true (different memory allocations)

Primitive Optimization

When the != operator is used on Kotlin’s basic types (such as Int, Double, Boolean) that are not nullable, the compiler optimizes the operation. Instead of boxing the primitives and invoking equals(), it compiles directly down to the JVM’s native bytecode instructions for primitive comparison (e.g., if_icmpne), ensuring zero allocation overhead.
Master Kotlin with Deep Grasping Methodology!Learn More