在Java中使用带有String和Object的equals()方法 [英] Using the equals() method with String and Object in Java
问题描述
Object o1 = new Object();
Object o2 = new Object();
//o1=o2;
System.out.println(o1.equals(o2));
它返回 false
。如果评论被删除,它可以返回 true
。
It returns false
. It can return true
, if the comment is removed.
为什么不适用于 String
类?
String s1=new String();
String s2=new String();
System.out.println(s1.equals(s2));
它返回 true
。为什么? (因为字符串
使用实习或其他相关内容?)
It returns true
. Why? (because String
uses interns or something else involved?)
推荐答案
因为 字符串的
Because equals() for String compares the content, not the object itself.
public boolean equals(Object anObject)
public boolean equals(Object anObject)
将此字符串与指定的字符串进行比较宾语。当且仅当参数不为null并且是 String
对象时,结果才为真,该对象表示与此对象相同的字符序列。
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String
object that represents the same sequence of characters as this object.
/* String.equals() */
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
(链接到 String .equals())
Versus the equals for Object:
类
Object
的equals方法实现了对象上最具辨别力的等价关系;也就是说,对于任何非空引用值x
和y
,当且仅当<$时,此方法返回true c $ c> x 和y
引用同一个对象(x == y
值true
)。
The equals method for class
Object
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesx
andy
, this method returns true if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).
/* Object.equals() */
public boolean equals(Object obj) {
return (this == obj);
}
(链接到 Object .equals())
另外,不要忘记的合约等于()
函数:
equals方法在非空对象引用上实现等价关系:
The equals method implements an equivalence relation on non-null object references:
- 自反:对于任何非空参考值
x
,x .equals(x)
应该返回true。 - 对称:对于任何非空引用值
x
和y
,x.equals(y)
当且仅当y.equals(x)$时才会返回true c $ c>返回true。
- 传递:对于任何非空引用值
x
,y
和z
,如果x.equals(y)
返回true
和y.equals(z)
返回true
,然后x.equals(z)
应返回true
。 - 一致:对于任何非空参考值
x
和y
,多次调用x.equals(y)
始终返回true
或一致返回false
,前提是未修改对象的等比较中使用的信息。 - 对于任何非空引用值
x
,x.equals(null)
应该返回false
。
- 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
andy
,x.equals(y)
should return true if and only ify.equals(x)
returns true. - It is transitive: for any non-null reference values
x
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
. - It is consistent: for any non-null reference values
x
andy
, multiple invocations ofx.equals(y)
consistently returntrue
or consistently returnfalse
, provided no information used in equals comparisons on the objects is modified. - For any non-null reference value
x
,x.equals(null)
should returnfalse
.
还建议阅读:
- Object.hashCode()
- Effective Java (Bloch)
这篇关于在Java中使用带有String和Object的equals()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!