This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashCode and Equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashCode and Equals" Watch "HashCode and Equals" New topic
Author

HashCode and Equals

Ravi Yadav
Greenhorn

Joined: Mar 02, 2008
Posts: 29
This Q is from K&B masterExam CD:


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

Which of the following will fullfill the equals() and hashCode() contracts for
this class? (Choose all that apply)

a. return ((SortOf)o).bal == this.bal;
b. return ((SortOf)O).code.length() == this.code.length();
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;

Answer is c,d

Can someone please explain how d is correct?

if we have two instances of the class SortOf A and B with the following parameters

A
------
code.length() = 5
bal = 4
rate = 2

B
--------
code.length() = 5
bal = 2
rate = 4


using option D would make the two instances A and B equal i.e 5*4*2 = 5*2*4
Where as their hashcode would be different (5*4) != (5*2)

THIS BREAKS THE HASCODE, EQUALS CONTRACT that if two objects are equal, their hashcodes have to be equal.

In case a case how can D be correct?
Manju Kavi
Ranch Hand

Joined: Sep 25, 2008
Posts: 33
Hi,
If we take 2 instances with values you have provided, they are not meaningfully equal, meaning the values of the instance fields of 2 instances (A and B) are not equal. The purpose of overriding hashCode() and equals() methods is to avoid duplicate values and fast searching. So even though equal method returns true and hashCode method doesn't, these are not considered as duplicate values in collection and always hashcode method is called before equals method..
subhasish nag
Ranch Hand

Joined: Apr 25, 2008
Posts: 101
So even though equal method returns true and hashCode method doesn't,

Hi Manju,
I suppose hashcode must return same value if equals return true.


Thanks,<br />Subhasish
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

what Manju is trying to say is that in this example hashCode will return different values for two equal objects. This breaks the contract and this is why the code is behaving abnormally.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ravi Yadav
Greenhorn

Joined: Mar 02, 2008
Posts: 29
Manju/Ankit,

Doesnt D break the contract? I though the contract says if two objects are equal, their hashcodes should be equal too.

In this case the two objects A, B are equal because the equal methods return true. As per the contract, the hashcodes should be true as well...

what am I missing here...
Ravi Yadav
Greenhorn

Joined: Mar 02, 2008
Posts: 29
I guess I missed the meaningfully equivalent part...
What does meaningfully equivalent means? who decides what's meaningfully equivalent? I thought the purpose of overridding equals() method was to take control on the factors that make two objects equal. In the case of A,B one might consider the two instances equal if the product of their fields are equal and not the individual fields. In such a case I cannot use the given hashcode right?.

Please advice....
Amardeep Salkar
Ranch Hand

Joined: May 22, 2008
Posts: 46
Originally posted by Ravi Yadav:
I guess I missed the meaningfully equivalent part...
What does meaningfully equivalent means? who decides what's meaningfully equivalent? I thought the purpose of overridding equals() method was to take control on the factors that make two objects equal. In the case of A,B one might consider the two instances equal if the product of their fields are equal and not the individual fields. In such a case I cannot use the given hashcode right?.

Please advice....


Dear Ravi,

I think option (a) and option (b) are correct answers. Insted of C and D.

Regards,
Amardeep
 
Consider Paul's rocket mass heater.
 
subject: HashCode and Equals