Kotlin : ”==” vs “===” for Int and String

Xuda Lu
2 min readDec 19, 2020

--

== (Structural Equality)

“==” operator is used to compare the data of two variables.
Please don’t misunderstand this equality operator with the Java == operator as both are different. == operator in Kotlin only compares the data or variables, whereas in Java or other languages == is generally used to compare the references. The negated counterpart of == in Kotlin is != which is used to compare if both the values are not equal to each other.

===(Referential equality)

=== operator is used to compare the reference of two variable or object. It will only be true if both the objects or variables pointing to the same object. The negated counterpart of === in Kotlin is !== which is used to compare if both the values are not equal to each other.

Demo:

Result:

Question :

how to understand “For values which are represented as primitive types at runtime (for example, Int), the === equality check is equivalent to the == check.” from https://kotlinlang.org/docs/reference/equality.html

https://kotlinlang.org/docs/reference/equality.html

but i got different result in above Int Demo Test

I am confused ,hope any one can explain to me .

--

--

Xuda Lu
Xuda Lu

No responses yet