Referential and structural equality in Kotlin

后端 未结 1 788
不思量自难忘°
不思量自难忘° 2021-01-21 00:36

What is the difference between referential equality and structural equality in Kotlin?

val a = File(\"/myfile.txt\")
val b = F         


        
相关标签:
1条回答
  • 2021-01-21 01:16
    • Referential equality === (also called identity) means that the pointers for two objects are the same. That is to say the objects are contained in the same memory location which leads us to the fact that pointers reference to the same object.

      identity: determines whether two objects share the same memory address

    • Structural equality ==, in its turn, means that two objects have equivalent content. You should specify when two objects should be considered equal by overriding the equals() method.

      equality: determines if two object contain the same state.

    As well as in Java, in Kotlin there're no specific equals() and hashCode() generated by default (not considering data classes). Thus, until you've overriden these methods for your class, both == and === perform identity comparison.

    0 讨论(0)
提交回复
热议问题