Why are two objects with the same data not equal when using the equals () method

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 the objects contain the same data in .why variables? explain.

+6
source share
9 answers

Since, by default, an object checks for equality based on equal (Object obj) .

The equals method for the Object class implements the most varied possible equivalence relation for objects; that is, for any non-empty reference values ​​x and y, this method returns true if and only if x and y refer to the same object (x == y is true).

If you want to check equality with the equals() method in your class equal, you must override the equals () method of the Object class.
how-to-override-equals-method-in-java , as shown below:

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

And you should always override hashCode () whenever an override is equal to the method of the Object class.

# Effective Java, Joshua Bloch

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

+4
source

You need to override the 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 SHOULD PROVIDE THE CHARGE ()

+2
source

You need to override 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; } 
+2
source

You have not redefined equals . By default, the implementation of equals inherited from Object returns true if and only if two variables point to the same object.

Override equals to check if the field is equal (and hashCode if you are on it).

+1
source

The Object.equals () method checks to see if two LITERALLY things correspond to the same object. Although a1 and a2 contain the same information, they represent different objects in memory.

If you want to check the equality of information inside your objects, you can implement a class with the Comparable interface and override the compareTo method.

+1
source

Since you do not override .equals() (and if you do, you must also override .hashCode() ), you use the Object .equals() implementation; and this implementation returns true if and only if the references to the objects are equal (i.e. o1 == o2 ).

0
source

You need to override the Object.equals() method.

0
source

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

For more details see here. Comparing two objects using the equals method, Java.

0
source

The implementation in the object class, that is, the default implementation checks the links. Therefore, if the links are the same, it returns true, otherwise it returns false.

0
source

Source: https://habr.com/ru/post/947105/


All Articles