Kotlin Equality – Difference between “===” vs “==”

In the tutorial, JavaSampleApproach will introduce Kotlin Equality, the difference between === and ==.

I. Kotlin Equality

We have 2 types of Kotlin equality:
– Referential equality with === operation
– Structural equality with == operation

1. Referential equality

Referential equality is used to check references with same pointing object. It represents with === operation (negative form !==).

2. Structural equality

Structural equality is used to check equality of objects by equals() function with == operation (negative form !=)

a==b is translated to a?equals(b) ?: (b === null)

We can implement equals() function for object as below sample:


3. “===” vs “==”

=== should use to compare primitives while == is recommended to check equality of objects.
=== only returns true if both references point to same objects while == can return true or false base on override function equals().

II. Full Sourcecode

By JavaSampleApproach | January 8, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *