• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Collection - Chap 7 (Maps) (Problem in Understanding an example in K&B SCJP 1.6)

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



Output :
Dog key
New Dog Key
collection.Dog@35ce36
collection.Dog@757aef
null
null
New Dog Key
New Dog Key


Question
Object ref "d" is used as a key for the value "New Dog Key". the hashcode for object of class Dog,
is used as the length of variable "name".
As per the SCJP 1.6, Chap 7 , when the value of "name" for object ref "d" is changed to "magnolia" ie. d.name = "magnolia"; the object will be searched in a hash bucket other than that where the object was placed originally ie hash bucket returned when value of "name" was diego.

Hence, the System.out.println(m.get(d)); should return null!!
But, as you can see the code returns an output with value "new Dog Key".

I dont understand ?

 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read this or this...
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
be careful with function names
here you have hashcode() function, not hashCode(), as a result the program uses the default object hashCode() for d reference wich does not depend on any Dog field

In real life you can mark your overridden method by @Override annotation, wich will save you some nerve, but for exam we just have to be careful with such "typo's" in code
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic