Files
kotlin-fork/docs/confluence.jetbrains.com/Kotlin/40700138_Basic+operations.confluence
T
2012-05-30 14:41:40 +04:00

36 lines
1.3 KiB
Plaintext

h3. Equality
In [Kotlin] there are two types of equality:
* referential equality (two references point to the same object)
* structural equality (a check for {{equals()}}
h4. Referential equality
In [Kotlin], there's no built-in operator to check for referential equality, for we believe that it is rarely needed. Instead, there's an [inline function|Functions#Inline functions] {{identityEquals()}} that can be called in the following way:
{jet}
a.identityEquals(b)
// or
a identityEquals b // infix call
{jet}
And returns *true* if and only if {{a}} and {{b}} point to the same object.
h4. Structural equality
Structural equality is checked by the {{==}} operation (and its negated counterpart {{!=}}). By [convention|Operator overloading#Equals], an expression like {{a == b}} is translated to
{jet}
a?.equals(b) ?: b.identityEquals(null)
{jet}
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*.
Note that there's no point in optimizing your code when comparing to *null* explicitly: {{a == null}} will be automatically translated to {{a.identityEquals(null)}}.
h3. What's next
* [Control structures]
* [Function literals]
* [Returns and jumps]
* [Ranges]
* [This expressions]
* [Tuples]
* [Type casts]