Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# hashcode & equals question

Maleen Abeydeera
Greenhorn
Posts: 20
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

Ankit Garg
Sheriff
Posts: 9610
37
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
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: 9610
37
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
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: 9610
37
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
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

Himalay Majumdar
Ranch Hand
Posts: 324
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 ]