有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

对象类的java equals()方法

我在读{a1}并根据它

The method equals defines a notion of object equality, which is based on value, 
not reference, comparison.

但是当我打开Object类的equals方法声明时,它是:

public boolean equals (Object o) {
    return this == o;
}

但是在这里,引用是用Reference equality operator(==)检查的,这个声明如何与规范匹配


共 (1) 个答案

  1. # 1 楼答案

    javadoc for ^{}'s ^{}

    The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

    这意味着,如果你不在你的类中重写它,这就是你得到的

    JLS的说法仍然正确;但是,由实现定义自己的.equals()契约

    (当然,如果重写.equals(),也应该重写^{}