Improved Equality Check
Just like Java, Kotlin also has two types of equality checks:
-
equals() method in Java, or == operator in Kotlin, is a comparison of values, called structural equality.
-
== operator in Java, or === in Kotlin, is a comparison of references, called referential equality. Referential equality compares references and returns true if the two references are identical—that is, they refer to the same exact instance. The operator === in Kotlin is a direct equivalent of the == operator in Java.
But the structural equality operator == in Kotlin is more than the equals() method in Java. If you perform str1.equals(str2); in Java, you may run into a NullPointerException if the reference str1 is null. Not so when you use == in Kotlin. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access