File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java 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 » Java » Beginning Java
Bookmark "hashCode() and equals()" Watch "hashCode() and equals()" New topic
Author

hashCode() and equals()

Niranjan Prasad
Ranch Hand

Joined: Aug 13, 2005
Posts: 62
Why hashCode() must be overriden when equals() is overriden?
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Originally posted by Niranjan Prasad:
Why hashCode() must be overriden when equals() is overriden?


Because they both go hand-in-hand to determine the unique identity of an object. By definition, every object that is unique has a unique hash code, and every two objects that are equal have the same hash code.

Thus, if you are implementing your own way of testing equality, this consistency must still be maintained, for which you must also implement the hashCode() based on your specific logic for that class, such that the above requirements still hold.

Hope this clarifies it.


ASCII silly question, Get a silly ANSI.
 
GeeCON Prague 2014
 
subject: hashCode() and equals()