File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question about hashCode 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 "A question about hashCode" Watch "A question about hashCode" New topic
Author

A question about hashCode

Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
Hi,
I met a SCJP question. It said,

1. Equal objects must have equal hashCodes
2. Unequal objects must have unequal hashCodes
3. Both 1 and 2 are right.

The correct answer is 1. Then, 2 is wrong.
But, if unequal objects may have equal hashCodes, can I still
believe equals method ?

Thanks a lot.
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
if two objects are not equal according to equal method then there
hashcode may be equal or different.ideally it should be different but it is not requirement.

what you want to know by saying

i am not able to understand please elaborate.
Bram Maes
Ranch Hand

Joined: Sep 27, 2006
Posts: 32
It's not because objects have the same hashcode that these objects are equal. The hashcode() function is made to find an object quickly, not for testing equality. That's where the equals() method is used for. So although it isn't that good to have several different objects with same hashcode (regarding seeking time) it is perfectly legal.
Objects that are equal are obliged to have same hashcode.

Correct me if I'm wrong...


SCJP 5.0, SCEA part I
Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
Thank you, Bram

I made a mistake. I was always thinking that the method equals() just
use hashCode() to judge whether two references are sharing the
same object or not.

But, in "Thinking in Java 3rd", the author says "you should always override hashCode( ) when you override equals( ). " Who can tell me the relationship between the two methods?
Gowher Naik
Ranch Hand

Joined: Feb 07, 2005
Posts: 643
You should always override both equal and hashcode method if you are using collection that are using hashcode algorithm.
equal method is used to check equality of two objects and hashcode method is used to check hashcode of two objects when they are stored in hashcode bucket
To check object in hashcode bucket there are two steps
1.to check equality of two objects using equal method
2.to check hashcode using hashcode method
When both methods return postive result then object is located in hashcode bucket.If any one of above method will return negative result the object present in hashcode bucket will not be located.
This is why both methods are required to override.
joshua antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 117
"you should always override hashCode( ) when you override equals( ). "

If you override equals and do not override hashcode, then the contract of equal objects having same hashcode will be broken because the hashcode method of java.lang.Object will be invoked and will give different value for each object
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Object API document has detail explains and rules for equals() and hashCode().
Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
Then, I draw a conclusion that there is nothing but a contract between equals() and hashCode.

The remaining is how the method equals(Object o) in class Object be implemented, by comparing two objects' memory address?
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Originally posted by Song Gao:
Then, I draw a conclusion that there is nothing but a contract between equals() and hashCode.

The remaining is how the method equals(Object o) in class Object be implemented, by comparing two objects' memory address?


Howdy Song Gao,

I welcome you in Ranch, But unfortunately your name doesn't meet with naming policy.Please change your Screen Name accoding to Naming Policy of JAVA Ranch.

Moderators: Please take care of this Name.

Song---Song(Same as English Word)
Gao----Sing (Gao means Sing in english)

So that means Sing a Song.

Please take care of this issue.
[ October 06, 2006: Message edited by: A D Sharma ]

The Best way to predict your future is to create it - Every great individual common man
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about hashCode