• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Compare..

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic