aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hash code and equals 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 "hash code and equals" Watch "hash code and equals" New topic
Author

hash code and equals

Terrence White
Ranch Hand

Joined: Jun 26, 2002
Posts: 39
This is a question from Dan Chisholm's Exam:

Question 8
If two instances of a class type are not equal according to the equals method, then the same integer value must not be returned by the hashCode method of the two objects.
a. false
b. true

The listed answer is false.
Why is this not true?
[ October 07, 2002: Message edited by: Thomas Paul ]
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Although it would be preferable if unequal objects produce unequal hashcodes there are not always enough hashcode values to allow for one unique hashcode for each unique object value. The Hashcode method returns a value of type int. The set of unique values for the set of Long objects greatly exceeds the number of values of type int. For that reason, many objects of type Long that are not equal must share a single hashcode value.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
this month's JavaRanch newsletter has a great article on the equals and hashcode controversy.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
And here is the link:
Equals and hashCode by Manish Hatwalne


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: hash code and equals