• 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

Help me with equals! - I've exam tomorrow

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I make mock exam, and I have question:

class A {
String a;
int c;
short b;

public int hashCode() { return a.length() * c;}

public boolean equals(Object o) {
//insert code
}
}
Insert code, that fulfill contract between equals and hashCode


And one from correct answer is:
((A)o).a.lenght() * ((A)o).b * ((A)o).c == this.code.lenght() * this.b * this.c;

I don't understand why...
If o1 has code="a", b=2, c=3 and
o2 has code="a", b=3, c=2 then
o1.equals(o2) == true, but o1.hashCode() != o2.hashCode !!!

Please, explain me why it's corect answer....
 
Ranch Hand
Posts: 310
1
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Malgorzata, welcome to the ranch!

Could you tell me the source of this question? Also please use "Code" tags so that your code is more readable to others!
The rule is:
If hashCode() is same, equals() may be true, may not be true
If equals() is true, hasCode() needs to be same.


Good luck for the exam!


Thanks,
 
Malgorzata Klos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for Your reply.
I bought book by Kathy Sierra & Bert Bates with CD contains Master Exam. This question is from quiz A: question 52.
 
Sheriff
Posts: 9708
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
Malgorzata I believe you are right, the answer is wrong. I think this question has been bought up before too and there is definitely a mistake in the question...
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reposting the above code for clarification.



I don't understand why...
If o1 has code="a", b=2, c=3 and
o2 has code="a", b=3, c=2 then
o1.equals(o2) == true, but o1.hashCode() != o2.hashCode !!!



I do agree with your statement. In this case even though objects being equal is "true", hashCodes be equal is "false". This seems to violate the equals hashCode contract. May be I am overlooking something?
 
Malgorzata Klos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I attach oryginal question with answer. Maybe I rewrite something wrong?
But all time I don't think why D is correct - I hope, that I have'nt this question on exam.
Thanks for reply.
screen.JPG
[Thumbnail for screen.JPG]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output:-
true
true
code=a,b=3,c=3
code=a,b=2,c=3

Above example may help to understand the answer. In your current example hash code method implementation is not strong. but equals method making sure that object equality.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Kaushik,

I guess Malgo is write. He has given the following values and tested


If o1 has code="a", b=2, c=3 and
o2 has code="a", b=3, c=2 then



Though objects being equal, but their hashcode is returning false. So i think so there is a mistake in the question for sure.

Best Regards,

 
reply
    Bookmark Topic Watch Topic
  • New Topic