> ## 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.

# Kotlin Structural Inequality

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:

```kotlin theme={"dark"}
!(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()`:

```kotlin theme={"dark"}
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.

```kotlin theme={"dark"}
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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
