• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Please explain the last output of this example from kathy sierra book

Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here the last output ie. Dog key needs explanation. I agree that the

hashcode will have no problem here since both "clover" and "arthur"

have same length but when it comes to equals method, it checks the

name, so name clover and name arthur are different. So why is not

giving the output as null.
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you missed it when you changed d1.name to arthur..

when you say

the d1 object IS the key.. i.e.. you are giving the map a copy of reference to d1.. so now "dog key" 's key is "reference to d1".

do you get it?

k& b asks you to guess the output of this..

d1.name = "arthur";
System.out.println(m.get(new Dog("clover"))); // #3

so here, you are changing the d1's name to arthur, now the map has its key d1, which has name arthur.. when you call m.get(new Dog("clover")) , you'll be asking to find a key with clover as name, which doesn't exist because you changed d1's name to arthur... it returns null in that case..because now clover no more exists.. its

if you still don't get it, you might go here and see :

I had a similar doubt and the piece of code given by Andrew MonkHouse helped me..
    Bookmark Topic Watch Topic
  • New Topic