• Post Reply Bookmark Topic Watch Topic
  • New Topic
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

Mock question on HashCode

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the question from Mock:-

Given the following class, which are correct implementations of the hashCode() method?

1 return 0;
2 return a;
3 return a+b;
4 return a-b;
5 return a^b;
6 return (a16)|b;

Can anyone explain me the meaning of this question & whats the output.


[HENRY: Added Code Tags... BTW, in the future, please also mention which Mock is this question from.]
[ November 24, 2006: Message edited by: Henry Wong ]
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is about the hashCode and equals contract. You have to figure out which of the return statements are legal. For example...

return 0; is legal but not efficient. It returns the same hash every time
return a+b; is legal since it is a function of both a and b and the hashcode will be the same even when the numbers are negative.
return a-b; is illegal since the hashCode is different for negative numbers.

and so on...
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey John,
Could you be please clear on
return a-b; is illegal since the hashCode is different for negative numbers.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sathishkumar Ethiraju:
Hey John,
Could you be please clear on
return a-b; is illegal since the hashCode is different for negative numbers.



Basically, you are trying to follow the rule that, if the equals() methods returns true, the hashcode should be the same.

If you examine the equals() method, if (a=1, b=2) in one object, and (a=2, b=1) in another object, they should be equal -- according to the equals() method. However, if the hash code is "a-b", it will be positive in one case and negative in the other. So... using "a-b" as the formula for hashcode doesn't work.

Henry
 
I'm doing laundry! Look how clean this tiny ad is:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic