Equality
- Referential equality (two references point to the same object);
Structural equality is checked by the ==
operation (and its negated counterpart !=
). By convention, an expression like a == b
is translated to:
I.e. if a
is not null
, it calls the equals(Any?)
function, otherwise (i.e. a
is null
) it checks that b
is referentially equal to null
.
To provide a custom equals check implementation, override the equals(other: Any?): Boolean
function. Functions with the same name and other signatures, like equals(other: Foo)
, don’t affect equality checks with the operators ==
and !=
.
Structural equality has nothing to do with comparison defined by the Comparable<...>
interface, so only a custom equals(Any?)
implementation may affect the behavior of the operator.
Otherwise, the structural equality is used, which disagrees with the standard so that is equal to itself, and -0.0
is not equal to 0.0
.
See: .