Win a copy of Spring Boot in Practice this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

hashCode Question

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I saw this question on a mock exam, but I don't always trust their answers. I hear that hashCode questions are big on the certification exam, so a thorough explanation of this question would be helpful.
Given two instances of x and y, of a class that correctly implements equals() and hashCode() methods, which two are always equal? (Choose two)
A. (x.equals(y) == false) implies (x.hashCode(y) != y.hashCode())
B. (x.hashCode(y) != y.hashCode()) implies (x.equals(y) == false)
C. (x.hashCode(y) == y.hashCode()) implies (x.equals(y) == y.equals(x))
D. (x.equals(y) == true) implies (x.hashCode() == y.hashCode())
 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals/hashcode contract says:
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
Following this, options B and D should be correct.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think D must right!en......Maybe B is right!
This afternoon I'll take the exam!GOD bless me!
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Cutter:
A. (x.equals(y) == false) implies (x.hashCode(y) != y.hashCode())
B. (x.hashCode(y) != y.hashCode()) implies (x.equals(y) == false)
C. (x.hashCode(y) == y.hashCode()) implies (x.equals(y) == y.equals(x))
D. (x.equals(y) == true) implies (x.hashCode() == y.hashCode())


The Object.hashCode method does not declare a parameter so the code examples here are problematic.
 
Balloon Boo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
U're right,Dan!
I missed the hashcode() declare!
B is wrong!
 
Sudd Ghosh
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, oh... I too overlooked that. If that hashCode parameter was not there, then option B would have been correct, right ? Is there any other problem with that option?
Thanks, Sudd
reply
    Bookmark Topic Watch Topic
  • New Topic