Equality and identity
== operator in Java compares references for identity, in Kotlin it is internally converted to a call of the
The check for identity is possible with the new
=== operator (3x
One thing that probably most of us used to do wrong in the beginning is how to handle the
== operator in Java.
Contradicting the intuition of many, it does not compare the content to which the compared references refer but their actual values (read: memory addresses).
Instead, in Java, the
equals method is required to check the referenced content for equality.
Even if you’ve gotten used to it, Kotlin reminds you that using
== for the structural comparison (
equals) is actually cleaner and more sensible than just comparing the references.
So Kotlin internally converts
== to the call of
This means that in Kotlin you check instances with
== for structural equality instead of identity, as in Java.
As a consequence, you will only rarely encounter the call of
equals written out in Kotlin code, but rather the more intuitive and readable
If you want to check two references for identity, Kotlin offers the operator
=), which does just that.