aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode and equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcode and equals" Watch "hashcode and equals" New topic
Author

hashcode and equals

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
k&b says that equals and hashcode should use the same instance variables...
hashcode and the equals method should use the same instance variables


but the masterexam says that equals method must be atleast as precise as the hashcode method is..beacuse in the question equals uses more instacne varibales htan the hashocde method.now which one is right?


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
The official contract says that when 2 objects are equal, they have to have the same hashcode.

On the other hand, if two objects have the same hashcode, they don't have to be equal to each other.

That's the only big rule you have to stick to.


JDBCSupport - An easy to use, light-weight JDBC framework -
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

There's something to say about both statements. The equals method must return true when 2 objects are meaningful equal. Therefor not even all the instance variables have to be used.
Just follow these simple rules and you will be fine:

Equals must be:

Reflexive: x.equals(x) == true
Symmetric: x.equals(y) == true then y.equals(x) == true
Transitive: x.equals(y) == true and y.equals(z) == true then z.equals(x) == true
Consistent: Multiple calls must return the same result as long as none of the instance variables used in the equals method changed.

Hashcode:
x.equals(y) == true then x.hashCode() == y.hashCode()
x.equals(y) == false then x.hashCode() != y.hashCode()
This can not be mirrored. So when x.hashCode == y.hashCode, x.equals(y) does NOT have to be true


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
using equals() method without overriding, no two objects will ever be equal. because equals check that if two references are referring to the same objects.

equals() is a check for equality on two references while hashCode() is called to get hashcode value of one object.


SCJP 5.0 100%
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
Raju Champaklal wrote:k&b says that equals and hashcode should use the same instance variables...
hashcode and the equals method should use the same instance variables


but the masterexam says that equals method must be atleast as precise as the hashcode method is..beacuse in the question equals uses more instacne varibales htan the hashocde method.now which one is right?


k&B says they should, not must use same instance variables that means you can use other instance variables also while implementing equals method. But because it has to be atleast as precise as hashcode so you must use those instance variables used in hashcode method.


SCJP 1.6 96%
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
oh never thought grammar would be such a hindrance to the exam....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcode and equals