• 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

K&B page 448 equals() (not gc..sorry)

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5. What two statements are true about *properly* overridden hashCode() and equals() methods?
A. hashCode() doesn�t have to be overridden if equals() is.
B. equals() doesn�t have to be overridden if hashCode() is.
C. hashCode() can always return the same value, regardless of the object that invoked it.
D. If two different objects that are not meaningfully equivalent both invoke hashCode(), then hashCode() can�t return the same value for both invocations.
E. equals() can be true even if it�s comparing different objects.
Answers: c,e
My doubts:
[1] What does *properly* mean ? Legal / appropriate / efficent
[2] Could someone try to explain why c,e are right? I dont quite get the explanation given in the book.
Thanks,
Cathy.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Properly means appropriate - that doesn't violate the hashCode() and equals() contracts and, of course, compiles. Legal implementation just compiles, but may not be appropriate or efficient.
c is right because the same hashCode() will be returned for meaningfully equal objects (compared with equals()), which fulfills the contract. It's not efficient though because all objects of this type will end up landing in the same hash bucket.
e is right in case of comparison of different meaningfully equal objects, but instanceof validation in the equals() method must pass.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cathy Song:
[1] What does *properly* mean ? Legal / appropriate / efficent
[2] Could someone try to explain why c,e are right? I dont quite get the explanation given in the book.
Thanks,
Cathy.


Properly basically means it follows the contract of equals() and hashCode().
"If equals() returns true, then hashCode() for both the objects must be same"
Just understand this statement carefully, and any condition which does not violate the contract is legal/appropriate -> Proper
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic