Java 里的 equals 方法

324 天前
 lmybill

equals 本来比较的是两个对象是否是同一个对象,到 String 等类里却重写为比较值是否相等,这是不是改变了这个方法的原来意思,给人造成很大困扰

2287 次点击
所在节点    Java
29 条回复
lalawu
324 天前
要判断是否同一个对象直接用等号
msg7086
324 天前
equals 就是含义比较啊。

equals
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
Nooooobycat
324 天前
????

判断是否同一个对象,是用 == 呀(地址相同则是同一个对象)

.equal 就是判断两个对象的逻辑值是否相同的
totoro52
324 天前
都叫方法了,那具体实现不就看个人了
twofox
324 天前
因为字符串会放进常量池,是会复用的,没有影响
sentinelK
324 天前
“equals 本来比较的是两个对象是否是同一个对象”
恰恰相反,对比两个变量是否引用一个对象用 ==

equals 是一个可重写的方法,你愿意比较什么,就是什么。同理,toString()一样。
wolfie
324 天前
Override 是非常基础的概念,自己翻翻书吧。
TWorldIsNButThis
324 天前
是否同一个,那是比较 identity
对于数据库 entity 来说通常就是 id 字段
laoyur
324 天前
正文第一句话就错了
ruanimal
324 天前
这个问题和 java 反对运算符重载是一个意思
gaifanking
324 天前
我理解楼主的意思是 String 类的 equals 方法破坏了 Object 类的 equals 定义
但是我们看 Object 对 equals 返回的定义:
Returns: true if this object is the same as the obj argument; false otherwise.

这里面的 same ,在英语里即可以表示同一个,也可以表示一模一样(指两个)
再看 equal 的英文含义,即可以表示相同,也可以表示相等

所以并没有什么错误,有几楼没理解吧 扯了一堆没用的。
lmybill
324 天前
override 可以修改具体实现,但是不能修改方法的含义。对象的相等和对象里属性的值相等不应该是同一个意思。或者说 equals 这个方法本身定义就有点问题,模糊不清
JasonLaw
324 天前
我不明白你是怎么得出“equals 本来比较的是两个对象是否是同一个对象”这个结论的,可以给一下你的论证吗?

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Object.html#equals(java.lang.Object)

文档上是这么说的。

The equals method implements an equivalence relation on non-null object references:

* It is reflexive: for any non-null reference value x, x.equals(x) should return true.
* It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
* It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
* It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
* For any non-null reference value x, x.equals(null) should return false.
LeegoYih
324 天前
#12
按你这么说还要 equals 干嘛?这 API 删了,都用==不就完了?
没重写当然是对比地址,重写了就按重写了的逻辑达成共识,对比值用 equals ,对比对象用==
JasonLaw
324 天前
@JasonLaw 它后面也说了

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). In other words, under the reference equality equivalence relation, each equivalence class only has a single element.
Morii
324 天前
🤣 就是大家对 equal 的诉求不一样,所以才给你重写的机会呀
nothingistrue
324 天前
java.lang.Objects#equals Javadoc 的标题是:「 Indicates whether some other object is "equal to" this one.」 楼主脑回路不正常,理解成了「 Indicates whether some other object and this one is "the same one".」 然后接下来后面的人也照着这个思路去解释了。这真应了那句话:不要和白痴争辩,因为他会把你的智商拉到和他同一水平,然后用丰富的经验打败你。
x77
324 天前
坏消息是别的语言也这么定义:equals 用来判断对象的内容是否相同,== 用来判断是否同一个对象。
好消息是多数语言的 String 都提供 Compare 方法。
NeroKamin
324 天前
麻烦问问题之前先看看文档好吗,谁说 equals 是用来比较两个对象是否是同一个对象的,你说的那个是==
m2276699
324 天前
@nothingistrue 哥们,何必要攻击别人

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/946946

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX