Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

equals() and hashcode() from K&B

Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following,
11. x = 0;
12. if (x1.hashCode() != x2.hashCode() ) x = x + 1;
13. if (x3.equals(x4) ) x = x + 10;
14. if (!x5.equals(x6) ) x = x + 100;
15. if (x7.hashCode() == x8.hashCode() ) x = x + 1000;
16. System.out.println("x = " + x);
and assuming that the equals () and hashCode() methods are property implemented and x1 �
x8 are all of the same type, if the output is �x = 1111�, which of the following statements
will always be true?
A. x2.equals(x1)
B. x3.hashCode() == x4.hashCode()
C. x5.hashCode() != x6.hashCode()
D. x8.equals(x7)

The answer was...

1. B. By contract, if two objects are equivalent according to the equals() method, then
the hashCode() method must evaluate them to be ==.
A is incorrect because if the hashCode() values are not equal, the two objects must not
be equal. C is incorrect because if equals() is not true there is no guarantee of any result
from hashCode(). D is incorrect because hashCode() will often return == even if the
two objects do not evaluate to equals() being true.

I can't undertand why the answer given was like this.
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paulo,
The reason is because if the objects are equal by equals(), they will always return the same hashcode. But if the objects are not equal by equals() it doesn't mean that they may return different hashcode.Another thing which can be concluded is that if the hashcodes are not same they will never be equal.Hope this clarifies your doubt.
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider we have 3 set of different color balls.each set contains 3 balls.
Red-3,blue-3, totally we have 9 balls.Consider these 9 balls differ in weight.i want to store these balls in somewhere and whenever i require the particular weight ball,i need to fetch the correct ball.

so instead of putting all the balls in one bucket,if we store these balls in different bucket,it will be easy for us to find and fetch the correct ball with minimum search i am going to store these 3 different color balls in 3 different buckets.

RED bucket contains-red balls
BLUE bucket contains-blue balls
WHITE bucket contains-white balls

if my requirement is that fetch 5kg red ball then, first i need to find the right bucket and search each ball which one is 5kg in weight.

it is easy for us to search only 3 balls in RED bucket.if we stored our 9 balls in one bucket,we would have searched long time to get the right ball.

so,in java perspective,consider Hashcode determines in which bucket the value has to be stored and from which bucket the value has to be retrieved.Here,the bucket may contain many values(like RED bucket contain 3 red balls).but all the values have same hascode(all the red balls stored in RED bucket) but they may differ(like RED bucket balls differ in weight) someway.

Analyze your question now,you can get the answer.
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got to reread the question for a couple of the concept is much clear. Thanks
    Bookmark Topic Watch Topic
  • New Topic