Equality
코틀린과 자바의 식별성과 동등성
kotlin java
Identity
It calls Referential equality.
- In Java, we use the
==to compare the identities of two objects. Notice that identity is something external. A reference is not part of the object it simply points to the object. Another important point is identity doesn't change over time: As I get older, I'm going through a lot of changes, but I'm still the same person.- In Kotlin, we use the
===(negated: !==). For values represented by primitive types at runtime (for example, Int), the===equality check is equivalent to the==check.
Equality
It calls Structural equality.
Equality refers to two objects being the same. Two objects being equal doesn't necessarily mean that they are the same object.
- In Java, we use the
equals()method to check if two objects are equal. This is also called structural equality.- In Kotlin, we use the
==(negated: !=). That same asequals()method in Java.
// a == b is translated to in Kotlin:
a?.equals(b) ?: (b === null) // a == null will be automatically translated to a === null.