7
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
  • but why negative vote? Commented Jun 12, 2013 at 15:04

9 Answers 9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Effective Java Lemma 8, as I remembered.
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()

Comments

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; } 

Comments

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

Comments

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.

Comments

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

Comments

0

You need to override Object.equals() method.

1 Comment

By default, it check this == obj.
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

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.