# Mock Question Equal & Hashcode

Ranch Hand
Posts: 664
This is a question from Khalid Mughal.

Given that the objects denoted by the parmaters overide the equals() and hashCode() methods appropriately, which return value are possible from the following method?

String fun(Object x,Object y)
{
return (x==y)+ " " + x.equals(y) + " " + (x.hashCode()==y.hashCode());
|

Select 2 correct answers

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..

Thanks.

Thirumalai Muthu
Ranch Hand
Posts: 75
"==" Will always return false for two different objects so the first one will always be false.

Here it is said that the equals() and hashcode() have been overridden appropriately so if x.equals(y) is true then x.hashcode()==y.hashcode() will always be true so the answer can be

false true true

OR

if x.equals(y) is false then x.hashcode() == to y.hashcode() can be true or can be false so so the answer can be

false false true
false false false(not in the options)

The option "b" is incorrect because if x.equals(y) is true then x.hashcode()==y.hashcode() has to be true.

"d and e" are incorrect because the first option cannot be true.
[ August 26, 2008: Message edited by: Thirumalai Muthu ]

Ankit Garg
Sheriff
Posts: 9519
22
Originally posted by Thirumalai Muthu:

"d and e" are incorrect because the first option cannot be true.

This means that if == is true than equals will always return true and hashCode will always return same hash codes....

ramesh maredu
Ranch Hand
Posts: 210

This means that if == is true than equals will always return true and hashCode will always return same hash codes....

x==y is true means both references pointing to same object so x.hashCode() and y.hashCode() will be same always, even though their actual value may vary from one execution to another.

Thirumalai Muthu
Ranch Hand
Posts: 75
Two objects will be "==" only if they are of the following types:

Boolean
Byte
Character
Short
Integer(only within some range)

If the object is other than these types mentioned then two instances(objects)
will always not be "=="

Ankit Garg
Sheriff
Posts: 9519
22
Originally posted by Thirumalai Muthu:
Two objects will be "==" only if they are of the following types:

Boolean
Byte
Character
Short
Integer(only within some range)

If the object is other than these types mentioned then two instances(objects)
will always not be "=="

What??? == will be true if the two references refer to the same object....What are you trying to say here...

Igor Lichs
Greenhorn
Posts: 15
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

imagine the following situation in the main method, assuming the fun method is in the TestClass:

that's why i suppose its 4 correct answers and not just 3.

please guys, correct me if i'm wrong, i'm here to learn

Ankit Garg
Sheriff
Posts: 9519
22
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

Yes you are right...but here we are discussing only the ones that are in the options....

Igor Lichs
Greenhorn
Posts: 15
i'm thinking now, why letter b is wrong? why the hashCodes MUST be the same? cant I implement a method which gives me different hashCodes?

HungryJavaGoat
Greenhorn
Posts: 23
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.

Ranch Hand
Posts: 664
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...

Igor Lichs
Greenhorn
Posts: 15
Ok, check this out: http://java.sun.com/javase/6/docs/api/java/lang/Object.html#hashCode()

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.

Ranch Hand
Posts: 664
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

x==y true -> x.equals(y) true
x==y true -> x.equals(y) false
x==y false -> x.equals(y) false

But i don't think this is possible

x==y false -> x.equals(y) true

Can some one help here...?

Igor Lichs
Greenhorn
Posts: 15
in letter c, it's false true true

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.

Vyas Sanzgiri
Ranch Hand
Posts: 686
== 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

So, a and c are the possible return values

Ranch Hand
Posts: 664
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 ]

Igor Lichs
Greenhorn
Posts: 15
Sure.

If you run this code, the answer will be false false true