This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Hashcode basics Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Hashcode basics" Watch "Hashcode basics" New topic
Author

Hashcode basics

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131


Q:-Which of the following options may be insterted at //1?

[option 1] "return theval%2 == 0? true :false;" // incorrect whereas [option2 ]"return theval%3 == 0? true :false;" //is given correct.


with the explanation:-Option 2 is correct because hashCode() for all the multiples of 3 is 0. Therefore, if we return true in the equals() method for all the multiples of 3, the condition will be met.
Option 1 is not correct because then 2 and 6 would be considered equal, but their hash codes will be different (2 and 0).

can I pl. know what's it all about when reading "...correct because then 2 and 6 would be considered equal, but their hash codes will be different (2 and 0). "
Seema Manivannan
Ranch Hand

Joined: Sep 20, 2001
Posts: 128
If two objects are equal, their hashcodes must also be the same. So when we define the equals and hashcode methods, we need to ensure that this condition is always true. So in the given example, if you define the equals method like this - "return theval%2 == 0? true :false;" - you are saying that all objects which have theVal as a multiple of 2, are equal. However the hashcodes ,might not be the same, as in the case where theVal is 2 and theVal is 6.


Seema Manivannan<br />Author and Trainer: Java Certifications<br /> <br />Whizlabs Software<br />Success, certified!<br />Global leader: J2EE certification exam preparation!<br /><a href="http://www.whizlabs.com" target="_blank" rel="nofollow">http://www.whizlabs.com</a><br /> <br />An ISO 9001-2000 certified company!
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
would you be saying that 6 is not okay, since it'a also a multiple of 3 ?
and it says that their #code will be different (2 and 0) , how is this # code getting calculated ? ,
similiarly how is it being said that #code for all multiples of 3 is 0 ?
I asume I am missing something basic here.
Bruce Evans
Greenhorn

Joined: Nov 28, 2004
Posts: 14
Start with the premise that two Objects are equal as determined
by the equals() method. Given this, the hashCode() method MUST
return the same value for each Object.

Then, from the answer options given, you can see why option 2 is correct.
-Bruce

Originally posted by Netty poestel:
would you be saying that 6 is not okay, since it'a also a multiple of 3 ?
and it says that their #code will be different (2 and 0) , how is this # code getting calculated ? ,
similiarly how is it being said that #code for all multiples of 3 is 0 ?
I asume I am missing something basic here.
Seema Manivannan
Ranch Hand

Joined: Sep 20, 2001
Posts: 128
hi Netty

This is the code to calculate hashcode as per your example.

public int hashCode()
{
return theval%3;
}

What are the values you get for hashcode when theVal=2 and theVal=6 ?
Don't you get different values ? Similarly, theVal%3 would be zero for all the multiples of 3. Right ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you want the low down on hashcodes: check out Yes, he's been there - Corey's Hashcode Article!

And another by Manish H.
[ December 06, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hashcode basics
 
Similar Threads
Section 9 Mock test questions
Please Explain (Hashcode)
hashcode,equals()
equals() method-Unable to understand
Equals( ) method semantics