Win a copy of Rust Web Development this week in the Other Languages forum!
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:
• Tim Cooke
• Campbell Ritchie
• Ron McLeod
• Liutauras Vilda
• Jeanne Boyarsky
Sheriffs:
• Junilu Lacar
• Rob Spoor
• Paul Clapham
Saloon Keepers:
• Tim Holloway
• Tim Moores
• Jesse Silverman
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Piet Souris
• Frits Walraven

# hashcode & equals question

Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
I got this question from Practice exam of Kathy Sierra book CD.
Q-52

clss Sortof{
String name;
int bal;
String code;
short rate;
public int hashCode() {
return (code.length() * bal);
}
public boolean equals(Object o) {
// insert code
}
}

Which of the following would fulfill the equals() and hashCode() contracts of the class?

A & B irrelevant
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;

The answer given is C & D.
but in D, isn't it possible for the hashcode to be not equal, and cause equals() to return true? Isn't that a violation of the contract?
eg:-
Sortof1 - code.length() = 2
bal =3
rate = 4
Sortof2 - code.length() = 3
bal = 4
rate = 2

Sheriff
Posts: 9691
42
• Number of slices to send:
Optional 'thank-you' note:
Hi Maleen, welcome to javaranch...

Yes Maleen, you are right. This is an error in that question. The answer is indeed C only....

Maleen Abeydeera
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
Thanks for your prompt answer Ankit. I have another question on this topic.

Is this equals() and hashCode() contract something you must adhere to? I mean, can there be a legal program such that
x5.equals(x6) == true
but x5.hashcode() != x6.hashcode() ?

It occurred to me from Q-53 of the same exam.
if x5.equals(x6) == true , then
x5.hashcode() == x6.hashcode() will "always" be true

Ankit Garg
Sheriff
Posts: 9691
42
• Number of slices to send:
Optional 'thank-you' note:
Well if you don't follow that contract, you might face unexpected outputs when you use hash based collections like HashMap or HashSet etc.

Then you will not be able to find objects which are meaningfully equal...

Maleen Abeydeera
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
ya, apart from the unexpected output, is there anything that will not work?

i mean, for the exam, are we supposed to treat this in the same league as a compilation error or an exception?

Ankit Garg
Sheriff
Posts: 9691
42
• Number of slices to send:
Optional 'thank-you' note:
No I don't think exam question will treat this as an error. There are questions like

Assume that XYZ class implements equals and hashCode correctly. Then what will happen when you run this code

or like this

what will happen when you run this program

and the program will not fulfill the contract so the output will be unexpected. But this doesn't mean that it will be an error. You have to choose the correct output which will come when the contract is broken..

Maleen Abeydeera
Greenhorn
Posts: 20
• Number of slices to send:
Optional 'thank-you' note:
ya, but what if it's a direct question?

like

if x5.equals(x6) == true , then is
x5.hashcode() == x6.hashcode() will "always" be true ?

without saying anything about whether the contracts are followed

Ranch Hand
Posts: 324
• Number of slices to send:
Optional 'thank-you' note:
No. Its not neccessary for them to be true. You can break the contract and still run it.

In your previous post, Contract is broken in the code but the program runs with unexpected output.

But as you know, its always advisable to stick to the contract
[ November 10, 2008: Message edited by: Himalay Majumdar ]

 Don't get me started about those stupid light bulbs.