wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() and hashcode() contracts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals() and hashcode() contracts" Watch "equals() and hashcode() contracts" New topic
Author

equals() and hashcode() contracts

Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
hey while going through one post i got one doubt....is it neccessary that we should use the same variables to implement equals() and hsahscode() methods.


Is it specifically mentioned in the contract that we should use the same variable for both hashcode() and equals() method.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Sanjit Kumar:
hey while going through one post i got one doubt...

I'm afraid that other post is something I said that's probably misleading.

No, it's not required that these methods use the same variables. But if they don't, you need to be very careful. I just posted a follow-up response under that other topic, so see if that clears it up.

Sorry for the confusion.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
thanks marc for the quick reply,

the concept i know is as follows:
1> if a.equals(b) is true then it must be consistent meaning it should always give the same result unless.

2> if a.equals(b) is true then b.equals(a) should also be true.

3> if a.equals(b) true then hashcode() for both object must be same.

4> if a.equals(b) is not true then hashcode for both object may or may not be equal.

If anyhting wrong in above concpet please correct me and mention if something more is required.

thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Sanjit Kumar:
thanks marc for the quick reply,

the concept i know is as follows:
1> if a.equals(b) is true then it must be consistent meaning it should always give the same result unless.

2> if a.equals(b) is true then b.equals(a) should also be true.

3> if a.equals(b) true then hashcode() for both object must be same.

4> if a.equals(b) is not true then hashcode for both object may or may not be equal.

If anyhting wrong in above concpet please correct me and mention if something more is required.

thanks


There are a few more to remember...

5. a.equals(a) must always be true.

6. if a.equals(b), and b.equals(c), then a.equals(c) must always be true.

7. a.equals(null) must always be false.

Okay, most of these points are "obvious conclusions" from the other four. However, they are mentioned in the specification, so they may come up in the exam.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() and hashcode() contracts