a. false false true b. false true false c. false true true d. true false false e. true false true
My answer was ( with out looking at the options) it would be either
true true true ( Objects having same hashcode and equal) false false false (Objects having different hashcode and not equal) e.true false true (Object having same hashCode but not equal.
The answer given here is a ,c
The explanation is: b is eleminated since hashcode cannot claim inequality if the equals() method claims equality. d,e are eliminated since the equal method must be reflexive nd the hashCode() method must return consistently the same value during execution.
Can some one please shed some light here..
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
But the question says that hashcode and equals method have been implemented appropriately. So "implementing the hashCode in wrong way" scenario is not applicable here.
Joined: Nov 05, 2007
Originally posted by Igor Lichs: I think it has 4 correct answers:
- false false true - false true true
and other two which are not in the options:
- false false false - true true true
How did you come to the conclusion that the first two are right ie. - false false true - false true true
Can you please explain?
And what's the difference between (x==y) and (x.hashCode()==y.hashCode()
Aren't they both checking whether they are the same object reference? The only difference i see is that the hashCode() is returning an int to prove they are reference to the same Object. While == is returning a boolean value.
so in that case, i thought either both of them would be true or both of them would be false...
It 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."
So even if the objects are different, they can have the same hashCode, however, if they are equals, they MUST have the same hashCode, if the equals and the hashCode were appropriately overridden, which it was, according to the question title.
x==y just compares memory spaces. To be true, x and y MUST have to be the SAME object, not just the same type.
Therefore, 2 different object will be false on x==y, false on x.equals(y), but comparing hashCodes they can the same or not. But it's a possibility. So letter a is correct.
If I have 2 Objects CAR and both are MERCEDES, they are not in the same memory space, so x==y is false, but they are equals, so their hashCodes must be the same. So letter c is also correct.
This is the way I see this, at least.
Joined: Nov 05, 2007
I got your first point- different objects CAN have the SAME hashcode ie. be in the same bucket even if they are not equal.
If objects are not equal but have the same hashcode :
They are different obects (x==y false) They are not equal (x.equals(y) false) they have the same hashcode() ( hashCode() true)
I did not get the second part ie c
How can two objects point to the same object ie x==y -> true And still be unequal x.equals(y)-> false
hasCode is NOT the memory space where you allocate the object, so x==y is NOT in the very same memory space.
if x is dog1 and y is dog2, they are Dog objects, same properties, but they are NOT in the same memory space.
equals were correctly overriden, so it check the dog1 properties with the dog2 properties and its hashcodes. Does it match? Great! They are equals. If they are equals, their hashCodes MUST be equals, because the question said they were appropriately overriden, and it means the Dog class has overriden the hashCode with the number 123456789 for exemple, so every dog will have the hashCode 123456789.
Can a Cat class have the hashCode 123456789? YES. It's not memory position.
The only possible way to x==y be true, is when you call fun like this:
x = a y = a
so they are the very same object, so it is TRUE, they are equals, so it is TRUE and their hashCode is the same, so it is TRUE.
true true true is one possible answer, but its NOT in the options. I think I have confused you with my previous post, sorry about that.
== compares if two references refer to same object If x and y refer to same object then the other two conditions have to be true i.e true true true
If x and y dont refer to same object then either both of the remaining two conditions have to be true or both have to be false. Since when two objects are equal using the equals() method, they must have the same hashcode. But if two hashcodes are equal then it is not necessary that the objects have to be equal. i.e false true true false false false false false true
when two objects are equal using the equals() method, they must have the same hashcode. But if two hashcodes are equal then it is not necessary that the objects have to be equal. false true true false false false false false true
I still didn't get it (the statement in bold) Can you explain it using a different approach....
[ August 27, 2008: Message edited by: Nabila Mohammad ] [ August 27, 2008: Message edited by: Nabila Mohammad ]
Joined: Aug 08, 2008
If you run this code, the answer will be false false true