Java - 即使两个对象的内容相同,equals() 是否有可能 return false?
Java - Is it possible for equals() to return false, even if contents of two Objects are same?
我知道这是重复的问题,但没有正确提出该问题,所以我没有得到答案。
但我在一次采访中被问到这个问题。
我想知道这可能吗?如果是,谁能给我提供代码?
提前致谢。
是的,如果你对 equals 的实现不佳。
public boolean equals(Object o){
return false;
}
例如,或者,如果它们的类型不完全相同:
public boolean equals(Object o){
// o is an instance of a parent class, with exactly the same content. bad design, but possible.
if ( o == null ){
return false;
}
if ( !o.getClass().equals(this.getClass()){ // or a similar check
return false;
}
Child ot = (Child)o;
return this.content.equals(ot.getContent());
}
在java中,方法public boolean equals(Object obj)
继承自Object.class。由于所有 Java 对象都(最终)继承自 Object,因此它们也都继承了该方法。但是,Objectclass中定义的方法的实现是,equals
方法将return当且仅当被比较的两个对象是同一个实例。
public class WrappedString {
private final String str = "hello";
}
public void foo() {
WrappedString ws1 = new WrappedString();
WrappedString ws2 = new WrappedString();
System.out.println(ws1.equals(ws2));
}
上述代码片段的输出将是 false
,因为 ws1
只会等于它自身(例如,由于 equals
未被覆盖,因此对同一实例的其他引用)。
StringBuilder 这样做是因为它是可变的。不考虑内容,只考虑对象是否相同
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder();
a.equals(b); // false as they are not the same object.
对于所有对象数组也是如此
int[] a = {};
int[] b = {};
a.equals(b); // false, not the same object.
Arrays.equals(a, b); // true, contents are the same.
是的。您还可以覆盖 equals() 方法并使用它。
class Person {
private String Name;
public Person(String name){
this.name = name;
}
@Override
public boolean equals(Object that){
if(this == that) return false; //return false if the same address
if(!(that instanceof People)) return true; //return true if not the same
People thatPeople = (People)that;
return !this.name.equals(thatPeople.name); //return false if the same name.
}
}
我知道这是重复的问题,但没有正确提出该问题,所以我没有得到答案。 但我在一次采访中被问到这个问题。 我想知道这可能吗?如果是,谁能给我提供代码?
提前致谢。
是的,如果你对 equals 的实现不佳。
public boolean equals(Object o){
return false;
}
例如,或者,如果它们的类型不完全相同:
public boolean equals(Object o){
// o is an instance of a parent class, with exactly the same content. bad design, but possible.
if ( o == null ){
return false;
}
if ( !o.getClass().equals(this.getClass()){ // or a similar check
return false;
}
Child ot = (Child)o;
return this.content.equals(ot.getContent());
}
在java中,方法public boolean equals(Object obj)
继承自Object.class。由于所有 Java 对象都(最终)继承自 Object,因此它们也都继承了该方法。但是,Objectclass中定义的方法的实现是,equals
方法将return当且仅当被比较的两个对象是同一个实例。
public class WrappedString {
private final String str = "hello";
}
public void foo() {
WrappedString ws1 = new WrappedString();
WrappedString ws2 = new WrappedString();
System.out.println(ws1.equals(ws2));
}
上述代码片段的输出将是 false
,因为 ws1
只会等于它自身(例如,由于 equals
未被覆盖,因此对同一实例的其他引用)。
StringBuilder 这样做是因为它是可变的。不考虑内容,只考虑对象是否相同
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder();
a.equals(b); // false as they are not the same object.
对于所有对象数组也是如此
int[] a = {};
int[] b = {};
a.equals(b); // false, not the same object.
Arrays.equals(a, b); // true, contents are the same.
是的。您还可以覆盖 equals() 方法并使用它。
class Person {
private String Name;
public Person(String name){
this.name = name;
}
@Override
public boolean equals(Object that){
if(this == that) return false; //return false if the same address
if(!(that instanceof People)) return true; //return true if not the same
People thatPeople = (People)that;
return !this.name.equals(thatPeople.name); //return false if the same name.
}
}