Java 的 equals() 和 C 的运算符 == 有什么区别?
在有关使用typeid
的问题中 是C++,我建议可以用它来比较对象比较中的类型。我还没有看到它做了太多,但我记住了 Java 的 equals
。
进一步研究 Java,情况似乎是这样:有些人说的实际类应该比较两个对象,有人说 instanceof
是正确使用的工具,可能需要双重调度。当然,在某些情况下,两者之一绝对更合适,但至少 这两个选项都会被考虑。
在 C++ 中,OTOH,我几乎找不到比较实际类型的代码。在大多数情况下,使用双重调度(使用dynamic_cast
),并且我找不到任何人坚持在相等检查开始时进行快速类型比较是正确的事情。
我想知道为什么多态类型比较问题在 Java 中有两种可接受的解决方案,而在 C++ 中似乎只有一种被认为是最佳实践?是否存在显着的技术差异,或者只是方法不同?
注意:我的主张基于印象,而不是具体知识。如果他们是错误的,并且 Java 和 C++ 在这方面确实相似 - 或者由于上述以外的原因而不同,那么这显然是一个可以接受的答案。
In a question regarding the use of typeid
is C++, I suggested it could be used to compare types in objects comparison. I haven't seen it done much, but I had Java's equals
in mind.
Looking into Java a bit more, this seems to be the case: Some say the actual classes of the two objects should be compared, and some say instanceof
is the right tool to use, possibly with double dispatch. There are of course cases in which one of the two is definitively more suitable, but at least both options are considered.
In C++, OTOH, I could barely find code in which the actual types are compared. On most cases, double dispatch is used (with dynamic_cast
), and I couldn't find anyone insisting a quick type comparison is the right thing to do at the beginning of the equality check.
I'm wondering why the problem of polymorphic type comparison has two acceptable solutions in Java, while in C++ only one seems to be considered the best practice? Are there significant technical differences, or just different approaches?
Note: My claims are based on impression, not concrete knowledge. If they are wrong and Java and C++ are indeed similar in that aspect - or different for reasons other than the above, it will obviously be an acceptable answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在Java中,所有类型最终都派生自
Object
,并且Object
定义了一个虚函数
Object.equals(Object other)
,所以你可以将任何事物与其他事物进行比较,无论是否
这是否有意义。在 C++ 中,没有通用基础,并且
==
没有隐式定义。==
通常只是当有意义时被覆盖,用于比较
相同类型,如果你写了废话编译器会抱怨
代码。在存在继承层次结构的情况下,
当然,
==
是否有意义由作者决定(我通常不会,但也有很多例外),如果
那么,对于比较对象而言,这意味着什么
不同类型。在层次结构之内或之外:它
在
BigInteger
和之间支持==
可能是有意义的例如,
BigFloat
,即使这些类不相关遗产。
你在 C++ 中看不到这个问题讨论的原因是,
当然,因为你没有定义
==
除非有一些它的逻辑意义,然后你根据
逻辑意义。在Java中,通常必须定义
equals
无论如何,所以你必须“发明”一些意义,然后你就得到了
讨论发明的意义应该是什么。
In Java, all types ultimately derive from
Object
, andObject
defines a virtual function
Object.equals(Object other)
, so youcan compare anything with anything else, regardless of whether
it makes sense or not. In C++, there is no univeral base, and
there is no implicit definition of
==
.==
is normally onlyoverridden when it makes sense, for comparing objects of the
same type, and the compiler will complain if you write nonsense
code. In cases where there is an inheritance hierarchy, it is,
of course, up to the author to decide whether
==
makes sense(I usually doesn't, but there are a lot of exceptions), and if
so, what it should mean with respect to comparing objects of
different types. Within the hierarchy, or outside of it: it
might make sense to support
==
betweenBigInteger
andBigFloat
, for example, even if the classes aren't related byinheritance.
The reason you don't see the problem discussed much in C++ is,
of course, because you don't define
==
unless there's somelogical meaning for it, and then you define it according to the
logical meaning. In Java, you generally have to define
equals
regardless, so you have to "invent" some meaning, and you get
discussion over what the invented meaning should be.
一个明显的区别是 Java
equals
是一个虚拟方法(因为默认情况下所有 Java 方法都是虚拟方法),因此将根据其目标进行动态调度。C++
operator==
重载是静态解析的,但如果您想要多态行为,则可以轻松委托给虚函数。除了多态性的差异之外,所有其他行为完全取决于特定类型的实现者(或者在 C++ 情况下,独立
操作符==
的实现者)。An obvious difference is that Java
equals
is a virtual method (since all Java methods are by default), so will do dynamic dispatch based on its target.C++
operator==
overloads are statically resolved, but it's easy to delegate to a virtual function if you want polymorphic behavior.Except for the difference in polymorphism, all other behavior is completely up to the implementer of the particular type (or in the C++ case, the implementer of a freestanding
operator==
).Java 对所有引用类型都有一个基本类型——所有引用类型都扩展 java.lang.Object(对
null
取模,这会破坏equals
对称性)因为(null).equals(...)
是一个错误)。所以你可以在 Java 中说“这两个 java 引用是否指向相同的东西?”不知道任何有关引用类型的信息,因此 Java 有一个地方可以将其基本引用类型
java.lang.Object
的equals(Object)
方法挂在而 C++ 则不然。在 C++ 中,没有这样的基类型,因此有多种不同的==
运算符,并且编译器必须能够静态地找出要使用的运算符。由于 Java 对象始终携带 RTTI,并且所有对实例方法的分派都被指定为虚拟方法,因此在代码中定义等价类时,您可以使用反射完成一些使用 C++ 对象无法完成的操作。
Java has a single base-type for all reference types -- all reference types extend
java.lang.Object
(modulonull
which breaksequals
symmetry since(null).equals(...)
is an error).So you can say in Java "do these two java references point to equivalent things?" without knowing anything about the types of the references, so Java has a place to hang an
equals(Object)
method of its base reference type,java.lang.Object
in a way that C++ does not. In C++ there is no such base-type so you have a multitude of different==
operator and the compiler has to be able to figure out statically which to use.Since Java objects always carry RTTI and all dispatch to an instance method is speced as virtual, there are things you can do with reflection when defining equivalence classes in code that you simply cannot with C++ objects.
使 C++ == 等同于 Java 的 equals 假定您已重写 == 运算符以在 C++ 中执行“深度等于”操作,并重写 Java“等于”操作以执行相同操作。
Making C++ == equivalent to Java's equals assumes that you've overridden the == operator to do "deep equals" in C++ and the Java "equals" to do the same.