Kotlin中的参照和结构平等 [英] Referential and structural equality in Kotlin
问题描述
引用相等和结构相等之间有什么区别 在科特林中?
What is the difference between referential equality and structural equality in Kotlin?
val a = File("/myfile.txt")
val b = File("/myfile.txt")
val sameRef = a === b
和:
val a = File("/myfile.txt")
val b = File("/myfile.txt")
val both= a == b
推荐答案
-
引用相等
===
(也称为身份)表示两个对象的指针相同.也就是说,这些对象包含在相同的存储位置中,这使我们得出以下事实:指针引用了同一对象.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.
与 Java 一样,在 Kotlin 中,默认情况下没有生成特定的
equals()
和hashCode()
(不考虑As well as in Java, in Kotlin there're no specific
equals()
andhashCode()
generated by default (not considering data classes). Thus, until you've overriden these methods for your class, both==
and===
perform identity comparison.这篇关于Kotlin中的参照和结构平等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!