Why are two objects with same data not equal while using equals() method [duplicate]

廉价感情. 提交于 2019-12-01 03:38:24

问题


public class Account {

    String account;
    double balance;

    Account(String account, double balance) {
        this.account = account;
        this.balance = balance;
    }
}

public class AccountTest {

    public static void main(String[] args) {
        Account a1 = new Account("Sandy", 1000);
        Account a2 = new Account("Sandy", 1000);
        System.out.println(a1.equals(a2));
    }
}

When i execute it showing "false" but objects contains same data in variables.why?explain.


回答1:


Because by default object checks equality based on equals(Object obj).

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).

If you want to check equality with equals() method in your class equal you have to override equals() method of Object class.
how-to-override-equals-method-in-java, Like following:

@Override
public boolean equals(Object obj) {
   // your implementation 
}

And you should always override hashCode() whenever overriding equals method of Object class.

#Effective Java, by Joshua Bloch

You must override hashCode() in every class that overrides equals(). Failure to do so will result in a violation of the general contract for Object.hashCode(), which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.




回答2:


You need to override equals() method, and use it whenever you want to compare their values.

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

BUT WHY I HAVE TO OVVERIDE EQUALS()




回答3:


You need to overrides equals

@Override
public boolean equals(Object obj) {
    if (!(obj instanceof Account))
        return false;
    Account that = (Account) obj;
    return (account == null ? that.account == null : account
            .equals(that.account)) && balance == that.balance;
}

I almost forgot to override hashCode when overriding equals

@Override
public int hashCode() {
    int hash = 17;
    hash = 37 * hash + (account == null ? 0 : account.hashCode());
    long l = Double.doubleToLongBits(balance);
    hash = 37 * hash + (int) (l ^ (l >>> 32));
    return hash;
}



回答4:


You did not override equals. The default equals implementation, inherited from Object, returns true if and only if the two variables point to the same object.

Override equals to check for field equality (and hashCode, if you're at it).




回答5:


The Object.equals() method is testing to see if the two things being compared are LITERALLY the same object. While a1 and a2 contain the same information, they are different objects in memory.

If you want to test equality of the information inside your objects you can have the class implement the Comparable interface and override the compareTo method.




回答6:


Since you don't override .equals() (and if you do, you must also override .hashCode()), you use Object's .equals() implementation; and this implementation returns true if and only if the object references are equal (ie, o1 == o2).




回答7:


You need to override Object.equals() method.




回答8:


It would show true if a1.balance==a2.balance . Note that the equals() compares objects and not their actual values. To be able to compare an Object you have to overwrite the equals() method.

See here for more info Comparing two objects using an equals method, Java




回答9:


The implementation in Object class i.e. the default implementation checks the references. So if the references are same it returns true else it returns false.



来源:https://stackoverflow.com/questions/17064661/why-are-two-objects-with-same-data-not-equal-while-using-equals-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!