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
• Number of slices to send:
Optional 'thank-you' note:
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 !!!

Ranch Hand
Posts: 310
1
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
Hi,
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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.
screen.JPG

Ranch Hand
Posts: 102
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
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,