What does the === operator in Kotlin mean?

4

What does the Kotlin === operator mean, and how to use it? I found this snippet of code in the documentation, but I was not sure.

val boxedA: Int? = a
val anotherBoxedA: Int? = a
println(boxedA === anotherBoxedA) // !!!Prints 'false'!!!
    
asked by anonymous 04.12.2018 / 14:18

1 answer

6

It is used to check the referential equality, ie if the references contained in the two variables are the same, so they point to the same address and then the same object, which obviously the equal value will be equal too.

In your example you have objects of the same value, but they are totally different objects. The Int? is a type by reference so each variable has a different object. If it were only the Int would be different.

It is used in opposition to the% cos_de% that only analyzes the value of the object, so it is possible that two different objects in different places are equal if the analysis of their structure is equal. The way these values are scanned depends on each object and can be customized on the type you create. There are cases where the simple, or structural, equality is actually the same as the referential, but it is only because the type has established it so. This has to do with object identity.

documentation speaks this.

    
04.12.2018 / 14:26