[英]Why doesn't Java's `equals()` do deep comparison by default
It is well-known that the equals()
method of an object, if not overridden, is a "shallow comparison" that is equivalent to using the "==" operator. 众所周知,对象的
equals()
方法如果不被覆盖,则是“浅比较”,等同于使用“==”运算符。 (See, for example, https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html .) (例如,参见https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html 。)
Question: why doesn't Java provide a "deep-comparison" equals()
method by default? 问题:为什么Java默认情况下不提供“深度比较”
equals()
方法? That is, one that will invoke equals()
on each of its instance variables recursively. 也就是说,将以递归方式对每个实例变量调用
equals()
。 Eventually, the recursion will reach primitive types and stop. 最终,递归将达到原始类型并停止。 Are there any downsides if this deep-comparison equals was the default?
如果这种深度比较等于默认值,是否有任何缺点?
Are there any downsides if this deep-comparison equals was the default?
如果这种深度比较等于默认值,是否有任何缺点?
Yes. 是。 These include:
这些包括:
The current default implementation is fast and makes no assumptions. 当前的默认实现很快,不做任何假设。 It's a good default.
这是一个很好的默认值。 Sometimes you will need to override the default, using your knowledge about what the logical value of the object contains, regardless of its root physical representation.
有时,您将需要使用您对对象的逻辑值包含的内容的了解来覆盖默认值,而不管其根物理表示形式如何。
A deep compare is much more complex and time consuming than a comparison of two references. 与两个引用的比较相比,深度比较要复杂得多,也要耗费时间。 This might be ok for simple objects, but when you have really complex data structures (eg a tree with ten thousand elements) how should the system know how "deep" the compare should be?
这对于简单对象可能没问题,但是当你有非常复杂的数据结构时(例如一棵有一万个元素的树),系统应该如何知道比较应该“深入”?
For most objects reference equality is the correct implementation. 对于大多数对象,引用相等是正确的实现。 "Deep" equals is for the minority that maintain state.
“深层”等于维持国家的少数民族。 Not only would your proposal run into the problems many here described, it would be wrong for most types.
您的提案不仅会遇到许多此处描述的问题,对大多数类型而言都是错误的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.