• 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

SJCP - equals() question

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the mock questions in the MasterExam allows these two implementations of equals() (I've commented out the first)



I agree with the commented out version, but aren't there values for rate and bal which would break the hashcode contract for the uncommented version - if rate=1 and bal=2 in one object and rate=2 and bal=1 in the other? That way, equals would return true but hashcode false, assuming code has a non-empty string - or am I missing something?

Does anyone know if errata has been produced for MasterExams (from the Sierra and Bates CDROM)?
[ May 31, 2008: Message edited by: Colin Hillman ]
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Just hving a quick look at those equals() methods, I think you are correct. 6 and 4 or 4 and 6 would create such errors. That method would appear capable of returning true from two SortOfs which do not encapsulate the same data.
 
Colin Hillman
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks - yes you're right, although it might be a case of "equality is in the eye of the beholder" - i.e. we can define equality on any terms.

My concern is that it breaks the hashcode() contract since in the example you gave, x.equals(y) is true, but x.hashcode() != y.hashcode(). Am I right?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the same problem,

For me :

break the hashCode() contract.

Error in exam?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to run some test code, but if you can find a case where you can show a.equals(b) && a.hashCode() != b.hashCode() you have demonstrated that the class breaches the contract for hashCode.

Please try it and report.

Yes, it does appear that you probably both have the same problem, obviously with the same question.
 
Thank you my well lotioned goddess! Here, have my favorite tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic