File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Equals and HashCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Equals and HashCode" Watch "Equals and HashCode" New topic

Equals and HashCode

Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48
i was reading the K&B book, when i came to this

this code i made, says the contrary

thanks !
[ October 10, 2008: Message edited by: Juan Manuel Alberto de los Santos ]
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Your book must also say whenever you override equals(), you have to override hashcode() as well.

-Varun -
(My Blog) - Online Certifications - Webner Solutions
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
this code i made, says the contrary

Well, you can always create code breaking so called general contracts. The book specifies the conditions which have to be fulfilled to satisfy the general contract for java.lang.Object.hashCode() (have a look at the javadocs there).
It is necessary to satisfy the general contract to ensure that other classes relying on that contract work properly. For example, hash based collections like java.util.HashSet may not work properly if you break that contract. Do you know why?


SCJP 5.0, SCJD in progress
I agree. Here's the link:
subject: Equals and HashCode
It's not a secret anymore!