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

Chris Knight
  • 23,000
  • 24
  • 84
  • 132

9 Answers9

5

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.

Community
  • 1
  • 1
Sumit Singh
  • 23,530
  • 8
  • 71
  • 99
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()

Community
  • 1
  • 1
Suresh Atta
  • 114,879
  • 36
  • 179
  • 284
2

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;
}
johnchen902
  • 9,200
  • 1
  • 25
  • 63
1

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

Gabriel Negut
  • 12,932
  • 3
  • 34
  • 43
1

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.

Funkytown
  • 470
  • 1
  • 6
  • 14
0

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

fge
  • 110,072
  • 26
  • 223
  • 312
0

You need to override Object.equals() method.

vineet
  • 342
  • 2
  • 11
0

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

Community
  • 1
  • 1
MaVRoSCy
  • 16,907
  • 14
  • 76
  • 116
0

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.

Bourne
  • 1,687
  • 11
  • 30
  • 50