• 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

legal equal()

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
took from Master Exam A (SCJP6 book)


which of the following will fulfill the equals() and hashCode() contracts for this class?


Correct answers:

C: return ((SortOf)o).code.length()* ((SortOf)o).bal == this.code.length()*this.bal();

D: return ((SortOf)o).code.length()* ((SortOf)o).bal * ((SortOf)o).rate == this.code.length()*this.bal() * this.rate;

I think only "C" is correct answer..
example (with D answer):

this.code.length() = 2;
this.bal() = 1;
this.rate = 5;

((SortOf)o).code.length() = 5;
((SortOf)o).bal() = 1;
((SortOf)o).rate = 2;

o hashCode is 2
this hashCode is 5 (different hashcodes => different objects)

BUT:

this.equals(o) is TRUE! (10 == 10) => D is false

What do you think?
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, option c seems to be correct accordingly to me too.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes only C is the correct answer. There was a long discussion on this question some time ago and the outcome was that only C is correct...
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Option C is correct
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

I'm not very good at Java. But somehow i feel that, there is nothing wrong with the equals comparison in option D. it doesn't break any contact of equals comparison. So, both options C and D are correct.

i think the hashcode implementation is not correct in this particular case. As per the hashCode contract:

"If two Objects are equal as according to equals method then calling the hashCode() method on each of the two objects must produce the same integer result"

this doesn't say other way round. So, i think the equals comparison is correct in both the options. And the hashCode implementation is not correct.

PLEASE CORRECT ME IF I'M WRONG.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic