File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is hashcode and equals contract? 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 "what is hashcode and equals contract?" Watch "what is hashcode and equals contract?" New topic

what is hashcode and equals contract?

varinder mahajan
Ranch Hand

Joined: Jun 11, 2008
Posts: 47
what is hashcode and equals contract?

Beat the world,if you can.......
marc weber

Joined: Aug 31, 2004
Posts: 11343

This is described in the API, under java.lang.Object's hashCode() method.

"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
Mario Razec

Joined: Aug 01, 2008
Posts: 17
I understand that:

equals() using to comparison, refers to equivalence relations.
hashCode() is id number allocated to an object by JVM.
Contract: When you use equals() and hashCode() methods is necessary to override these methods

But checking please:
arulk pillai
Ranch Hand

Joined: May 31, 2007
Posts: 3371
The key thing to remember is that:

-- If two objects are equal then they should have the same hascode but the reverse is not true (i.e. If two objects have the same hashcode does not mean that they are equal)

-- You need to use the same attributes in your equals & hashCode methods

Look at the doco for further info.

[] | [Java job hunting know how] | [Java resumes]
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Besides checking about the implications of equals and hashcode values, the exam may simply ask that if you implement equals() method, which other method you should implement?

SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
I agree. Here's the link:
subject: what is hashcode and equals contract?
jQuery in Action, 3rd edition