TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
== operator in Kotlin evaluates structural equality. It determines whether the data or state of two objects is equivalent by delegating to the underlying equals() method, rather than checking if they occupy the same memory address.
Unlike Java, where == evaluates referential equality for objects, Kotlin’s == is an inherently null-safe wrapper around the equals() function.
Compiler Translation
When the Kotlin compiler encounters the== operator, it translates the expression into a null-safe method call.
Execution Mechanics
- Null Safety: The operator guarantees null safety without throwing a
NullPointerException. If the left-hand operand (a) isnull, the safe call operator (?.) yieldsnull. The Elvis operator (?:) then evaluates the right-hand side, checking ifbis alsonullusing the referential equality operator (===). - Method Dispatch: If
ais notnull, the operator invokesa.equals(b). This relies on the class’s specific implementation ofoverride fun equals(other: Any?): Boolean. - Default Behavior: If a class does not explicitly override
equals()(and is not adata class), the operator falls back to the implementation inherited fromAny, which performs a referential equality check. - Primitive Optimization: When comparing Kotlin types that map to JVM primitives (such as
Int,Double, orBoolean), the compiler optimizes the==operator directly into the JVM’s primitive==bytecode instruction. This bypasses theequals()method entirely, eliminating boxing and unboxing overhead. - Floating-Point Comparison: When comparing
FloatorDoubletypes statically typed as their specific types,==follows the IEEE 754 standard (e.g.,NaN == NaNisfalse). However, if the operands are statically typed asAnyorComparable,==falls back to theequals()implementation, which enforces a total order (e.g.,NaN == NaNistrue).
Structural vs. Referential Equality
Kotlin strictly separates value comparison from memory address comparison using two distinct operators:==(Structural Equality): Evaluates if the values or states are equivalent viaequals().===(Referential Equality): Evaluates if both references point to the exact same object instance in the heap.
Master Kotlin with Deep Grasping Methodology!Learn More





