Java equals()是否可能返回false,即使两个对象的内容相同?

Java equals()是否可能返回false,即使两个对象的内容相同?,java,Java,equals()是否可能返回false,即使两个对象的内容相同? 如果是,我如何在以下代码中证明: 在下面的示例中,p1.equals(p2)如何导致“false” 我知道错误实现的equals()和hashcode()方法可能会导致这种情况,但我想知道在什么数据集下equality会失败 public class Equalitytest { public static void main(String[] args) { Person p1 = new Person("a

equals()是否可能返回false,即使两个对象的内容相同? 如果是,我如何在以下代码中证明: 在下面的示例中,p1.equals(p2)如何导致“false”
我知道错误实现的equals()和hashcode()方法可能会导致这种情况,但我想知道在什么数据集下equality会失败

public class Equalitytest {

    public static void main(String[] args) {

    Person p1 = new Person("abc",12);
    Person p2 = new Person("abc",12);

//Is there any way which results in  
// assert p1.equals(p2) == false  
//What dataset will will satisfy the assertion to false?

    }

    public class Person {
    private String name;
    private int age;

    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.age != other.age) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 53 * hash + this.age;
        return hash;
    }
    }
您可以像这样重写equals()方法

public class Equalitytest {

    public static void main(String[] args) {

    Person p1 = new Person("abc",12);
    Person p2 = new Person("abc",12);

//Is there any way which results in  
// assert p1.equals(p2) == false  
//What dataset will will satisfy the assertion to false?

    }

    public class Person {
    private String name;
    private int age;

    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.age != other.age) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 53 * hash + this.age;
        return hash;
    }
    }
@Override
public boolean equals(Object obj) {
return false ;
}

它是合法的。

不清楚您的代码与您的问题之间的关系。
等于
是由您定义的。它必须是可重复的,并且与
hashCode
一致。我只有一段示例代码,我的问题是有效的,因为我想验证如果w.r.t指定的数据集,等式是否会中断。@vmr您的问题是什么。如果您想让两个具有相同内容的对象的
equals()
实现失败,那么它可以正常工作。使
p2
成为扩展
Person
的类的对象,它没有自己的属性,并分配
Person p2=newnewclass(“abc”,12)
。可能你的意思是在其中也有一个“return false;”,因为否则你发布的内容是非法的,甚至无法编译。
public class Equalitytest {

    public static void main(String[] args) {

    Person p1 = new Person("abc",12);
    Person p2 = new Person("abc",12);

//Is there any way which results in  
// assert p1.equals(p2) == false  
//What dataset will will satisfy the assertion to false?

    }

    public class Person {
    private String name;
    private int age;

    public Person(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.age != other.age) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 53 * hash + this.age;
        return hash;
    }
    }