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

# equals() method-Unable to understand

Ranch Hand
Posts: 178
• Number of slices to send:
Optional 'thank-you' note:
Source: Inquisition-SCJP 5

Question: Given the following class House, which code fragments can be added to complete the equals() method without breaking the contract for hashCode()?

One of the options is:
(E) windows + doors + chimneys == other.windows + other.doors + other.chimneys

This option is not the answer and the explanation given is:

E also breaks the contract, but for a more subtle reason. It is possible for the number of chimneys to compensate for a difference in the number of windows and doors, and return true from equals, yet produce a different hash code. For example, new House(2, 4, 3) and new House(1, 4, 6) would compare equal with E's definition, but would produce different hash codes (8 and 4 respectively).

I am not able to get how the hashcodes calculate to 8 and 4. As per the code, the hashcode should be 6 and 5 respectively, right? Because, hashcode() returns the value of windows+doors

Ranch Hand
Posts: 101
• Number of slices to send:
Optional 'thank-you' note:
I agree,it seems hashcode should be 6 and 5 respectively.

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

Originally posted by Rekha Srinath:

I am not able to get how the hashcodes calculate to 8 and 4. As per the code, the hashcode should be 6 and 5 respectively, right? Because, hashcode() returns the value of windows+doors

did you try running the program and find what is actual hashcode value displayed? just run it and you will get answer as 6 & 5.

may be explanation given hase printed wrongly

Rekha Srinath
Ranch Hand
Posts: 178
• Number of slices to send:
Optional 'thank-you' note:
Yes Ankit..I agree with your point.

But my doubt was in the answer provided for the question: ....but would produce different hash codes (8 and 4 respectively).

For new House(2, 4, 3) and new House(1, 4, 6), the hash codes would be 6 and 5 respectively.

So, the values 8 and 4 as given in the answer is wrong, which Harshit has also mentioned.

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

For example, new House(2, 4, 3) and new House(1, 4, 6) would compare equal with E's definition

E. windows + doors + chimneys == other.windows + other.doors + other.chimneys

new House(2,4,3) and new House(1,4,6) would not be equal according to E.
(2+4+3 == 1+4+6 ) would return false!

So the example given might be wrong! Anyways Option E is not correct.
For Example: new House(2,3,4) and new(4,2,3) would be equal according to E but their hashcodes are different(5 and 6 respectively).

Hope this is clear!

Ranch Hand
Posts: 320
• Number of slices to send:
Optional 'thank-you' note:
Somehow it looks to me like, when he created the hashCode() method as windows + doors, then later mentally took the two examples :

House(2,4,3) and
House(1,4,6)

he might have multiplied instead of added (like he did Windows * Doors