| Author |
Compare..
|
Ramanan Sathiyanarayanan
Greenhorn
Joined: Dec 20, 2005
Posts: 11
|
|
From one of the SCJP book i got this question. Can you please justify the answer given below. The answer is taken from the same book. Given that the objects denoted by the parameters override the equals() and the hashcode() methods appropriately, which return values are possible from the following method ? String func(Object x, Object y) { return (x==y) + " " + (x.equals(y)) + " " + (x.hashCode==y.hashCode()); } Select two correct answers, a) false false true b) false true false c) false true true d) true false false e) true false true Correct answer given in the book is a) and c).
|
 |
Melanie Jones
Greenhorn
Joined: Dec 16, 2005
Posts: 21
|
|
Key Rule here is that if any two objects are meaningfully equivalent (return a true while using the .equals() method) then their two hashcodes should be the same. So (x.equals(y)) is true then (x.hashCode()==y.hashCode()) is true. (x==y) indicates the two objects are pointing to the same location hence the objects have the same value and as per the rule above the same hashcodes!! I think with this you can figure out the results are a) and c). Also true true true is also possible. - Mel
|
 |
Kamal Joshi
Greenhorn
Joined: Jan 30, 2006
Posts: 9
|
|
fora) & c) Java API documentation for class Object, the hashCode() contract says: It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode() method on each of the two objects must produce distinct integer results. b) if equals() and hashcode() methods are implemented appropriately, if (x.equals(y))evaluates to true then(x.hashCode==y.hashCode())should also returns true "If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result." for d) & e) if (x==y) returns true then both references are pointing to same object (aliases) so equals method should also returns true and consequently the next expression(x.hashCode==y.hashCode() should also returns to true i hope it will help -Kamal
|
 |
Ramanan Sathiyanarayanan
Greenhorn
Joined: Dec 20, 2005
Posts: 11
|
|
Very Thanks for your answers. As per your explanation we can simply say d) and e) are wrong. Because if a==b is true, which means both are same object. So rest all should be true. I agree with c), a==b can be false but still these objects can have same meaningful data. Example new Integer(1).equals(new Integer(1)) will be true, even though both are different objects. Now the issue is with a) and b) What i think is, If a) is correct, then b) should be correct. If a) is wrong, then b) should be wrong. If you say a) is correct, that means if a.equals(y) can be false, but still a.hashCode == b.hashCode can be true. Why the other way is wrong ? This is what i am not able to get. Keep in mind a and b are different objects because as per a) and b) a==b is false.
|
 |
 |
|
|
subject: Compare..
|
|
|