programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Hashcode basics

Ranch Hand
Posts: 131
• Number of slices to send:
Optional 'thank-you' note:

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). "

Ranch Hand
Posts: 128
• Number of slices to send:
Optional 'thank-you' note:
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.

Netty poestel
Ranch Hand
Posts: 131
• Number of slices to send:
Optional 'thank-you' note:
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.

Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 128
• Number of slices to send:
Optional 'thank-you' note:
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 ?

Ranch Hand
Posts: 7729
• Number of slices to send:
Optional 'thank-you' note:
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 ]

 Don't get me started about those stupid light bulbs.