• 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

Question about hashcode()

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a code from Mughal's mock egine,
Given the following class, which are correct implementations of the hashCode() method?

Select the three correct answers from:
1. return 0;
2. return a;
3. return a+b;
4. return a-b;
5. return a^b;
6. return (a 16)|b;
I wonder what's the standard to get the correct answer of above? And what's the meaning of choice 6(I haven't seen such an expression before)?
Thanks for any help!
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule for hashCode is fairly simple.
(1) If the equals() method says that two objects are equal then they must have the same hashcode.
(2) Two objects may have the same hashcode if they are not equals
Option number 6 is invalid. It won't compile.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe the correct answers are 3,4 and 5.
The reason being that the hashcode method when properly implemented should make use of the same instance variables that the equals method uses to determine equality.
Since in this example the equals method uses both a & b then so should your hashcode method. Therefore 1 and 2 are wrong since 1 uses neither a or b and 2 only uses a.
I don't know what is going on in choice 6, it doesn't look like choice 6 would even compile to me.
By the way choices 1 and 2 are still correct for implementing hashcode function. They are legal choices. The hashcode function can basically be anything you want, but a good hashcode function should make use of the same vars as the equals
[ July 21, 2003: Message edited by: Damien Howard ]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Damien Howard:
The hashcode function can basically be anything you want, but a good hashcode function should make use of the same vars as the equals


Actually, this is incorrect. Look at the rules again that I listed. The equals method is saying that two objects are equal if:
- a and b of first equals a and b of second
or
- a of first equals b of second and b of first equals a of second
(1) is a correct answer because it passes the most important rule. Two equal objects will return the same hashcode. (Unequal objects will return the same hashcode but that is irrelevant.)
(2) is incorrect. Let's take an example. We have two objects:
first --> a=1, b=2
second --> a=2, b=1
These objects are equal by the equals method so they must return the same hashcode. If they return a, they won't return the same hashcode.
3) this is correct. Using the example from above, they would both return 3.
4) this is incorrect. using the example above,
first would return -1 and second would return +1.
5) this is correct because ^ returns the same value, independent of order.
first --> 0001^0010 = hashcode of 0000
second --> 0010^0001 = hashcode of 0000
6) gibberish
So the correct answers are 1,3,5
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that options 1,3 and 5 are correct implementation of hashCode method.
1. For any two object returning same hashCode will be correct thought it is not very efficient.
2. Just returning a is not going to be correct in case two objects are equal because of obj1.a == obj2.b and obj1.b == obj2.a
3. This is good because a+b is same as b+a
4. This is not good because a-b is not necessarily b-a.
5. This is good because a^b is same as b^a.
6. This will not compile.
[ July 21, 2003: Message edited by: Barkat Mardhani ]
 
Amanda Fu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks everybody! After your discussion I believe 1,3 and 5 are correct answers. And I have checked it's right!
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amanda,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy (first name, space, last name).
Thanks Pardner! Hope to see you 'round the Ranch!
 
reply
    Bookmark Topic Watch Topic
  • New Topic