posted 14 years ago
The above code can be found in K & B book at page no. - 623. I have modified the line 8th as per the code given in the book.
The following was the output found ::
chap7.Dog1@4
DOG
CAT key
Dog key
null
5
null
Dog key
null
Doubt :: My doubt is on line no. - 8 and 9.
On line no. 7-As per the book says that if we change the value of the object it returns null so, here as we have changed the value to "mangolia" and so the output is null. But if we go to line no. -8 . I am changing the value again to "arthur" which has length equal to 6(which is equal to the length of "clover"), so it'll succeed the hashcode() method but it should fail in equals() method and should return null. But my output is giving "Dog key". Why? Please let me know if am wrong in my understanding.
On line no. 9- The value is changed to "arthur" but to get the key, a new object of type Dog1 with name as "clover" is created. And now here it fails. I didn't get that why we are creating a new object? And if we are assigning it to "clover" then it should succeed and what is the purpose of changing the d1.name="arthur" ? Which two values are now getting compared? That is, "clover"(which is created by new in line no. 8) with "arthur" OR "clover"(which is created by new in line no. 8) with "clover"(Created earlier as d1)??
Can anyone of you please clear my doubt on it.
Thanks,
Saumya