This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes hashcode & equals question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "hashcode & equals question" Watch "hashcode & equals question" New topic
Author

hashcode & equals question

Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

Hi Maleen, welcome to javaranch...

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Maleen Abeydeera
Greenhorn

Joined: Nov 10, 2008
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.
According to the answer,
if x5.equals(x6) == true , then
x5.hashcode() == x6.hashcode() will "always" be true
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: Nov 10, 2008
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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

Joined: Nov 10, 2008
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

Joined: Sep 28, 2008
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 ]

SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hashcode & equals question
 
Similar Threads
K&B Master Exam Question 56
K&B Master Exam hashCode() Question
mistake in k&b hashCode()/equals() contract question
hashcode contract S&B question
Wrong Answer in Master Exam? [equals() and hashCode() Contract]