• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

equals and hashcode question

 
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone.

Trying a mock exam from coderanch, I fall into the question below:

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


I've selected two correct answers, but there is an answer provided by the mock that I think it is wrong. That is:



I don't think the statement above are true, because running the code below, I get the output

hashCode: true
equals: false



tks
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The documentation states

equal objects must have equal hash codes


It does not say that unequal objects must have unequal hash codes.
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hash Code determines which bucket an Object goes into. There can be different Objects in the same bucket.


Object 1 and Object 2 can evaluate to the same Hash Code and end up in the same bucket. However, Object 1 is not equal to Object 2.
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all reply.

But that's the point. When I was working on this question, I firstly was convinced about the concept that having two instances of the same class may have the same hashcode and be meaningfully unequal.
So, at this mock from coderanch, there was another answer that leads to the same situation (same hascode values and not equals) that follows:



Regarding this, the answers that follows must be considered valid to not break the hashCode's contract am I right?




tks
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This one will violate the contract. Consider the following:


Equals gives true, hashCodes are not equal.
 
Adolfo Eloy
Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all. I understand now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic