https://grokonez.com/kotlin/kotlin-equality-difference-referential-equality-vs-structural-equality
Kotlin Equality – Difference between “===” vs “==”
In the tutorial, Grokonez 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
==
operation1. Referential equality
Referential equality is used to check references with same pointing object. It represents with===
operation (negative form!==
).
// ###########################
// 1. referential equality
// ###########################
val str = "a"
val str1 = "a"
println(str===str1)
/*
true
-> a & b point to the same object
*/
val i = Integer(10)
val j = Integer(10)
println(i===j)
/*
false
-> i & j point to the difference objects
*/
val jack = Customer("Jack", 25)
var customer = jack;
println(jack === customer)
/*
true
-> Because 'jack' and 'customer' objects point to the same object
*/
customer = Customer("Jack", 25)
println(jack === customer)
/*
false
-> Because 'jack' and 'customer' objects point to the difference object
*/
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)
More at:
https://grokonez.com/kotlin/kotlin-equality-difference-referential-equality-vs-structural-equality
Kotlin Equality – Difference between “===” vs “==”
Top comments (0)