d1.name ="clover" System.out.println(new Dog("clover");// prints Dog Key as output
d1.name ="arthur" System.out.println(new Dog(clover"));// prints nulll
} }
OUTPUT given is
null Dog Key null
My doubt is why is first one printing null? when we changed the key object d1's name to magnolia, it should get reflected in the Map too right?, As I understand it is the reference address of the object that gets stored in the collection(not the object itself). So when hashcode of magnolia is checked it should give 8. and the key object has name as magnolia and its hashcode should match. ??? Am I missing something???
Or, is it that the bucket number where the Object is stored does not change, based on any changes in the key object, once it is stored???
SCJP 5.0<br />SCJD working on it
ahmed yehia
Ranch Hand
Joined: Apr 22, 2006
Posts: 424
posted
0
Or, is it that the bucket number where the Object is stored does not change, based on any changes in the key object, once it is stored???
Yes, the Bucket's hashCode doesnt change inside the Map once its stored.
Mary John
Ranch Hand
Joined: Sep 17, 2007
Posts: 108
posted
0
Thanks very much Ahmed
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Hi Mary,
I have a question about the output mentioned.
Why does the first println display Dog Key. We are actually displaying a new object of Dog class with name "Clover". Same case with the second println statement. Just in case we have overriden toString() method, displaying the name, we would get "Clover" and "Clover" as the output. How did you get the output as Dog Key and null. Can you cross check that again?
Yeah, I understood that. But I was worrying if Mary had forgotten to include m.get(new Dog("Clover")); rather than just new Dog("Clover"); I guess it might be a typo error.
Mary John
Ranch Hand
Joined: Sep 17, 2007
Posts: 108
posted
0
Hi Chandra,
Thanks for pointing out that. that was a typo error....