• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

hashCode()-> reference,Object

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not able to figure out when I put an object as a key into a map and retrieve it I will get value as null,but
when I put a reference pointing to an Object as a key into a map and retrieve I get value.
Intentionally I didn't add equals()..


output:
hashCode():::8567361
hashCode():::9584176
null

hashCode():::19972507
hashCode():::19972507
java
Any suggestion on this?

Edit1: to paste output
Edit2:In my comment I changed null to value and viceversa..
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dileep keely wrote:I am not able to figure out when I put an object as a key into a map and retrieve it I will get corresponding value,but
when I put a reference pointing to an Object as a key into a map and retrieve I get null as value.
Intentionally I didn't add equals()..



There is no such distinction as "put an object as key" and "put a reference pointing to an Object as a key". In both cases, you are passing a reference to an object to the map, but in one case, you are using a declared reference, and in the other, the compiler is creating an anonymous reference for you.... ie. there is little difference between...



and...



Henry
 
dileep keely
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry!!
But, why do I get null in the first case.I was expecting the value("java").
Sorry for my understanding..
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dileep keely wrote:Thanks Henry!!
But, why do I get null in the first case.I was expecting the value("java").
Sorry for my understanding..



It's returning null because you are using the equal() / hashCode() methods of the Object class -- which by default considers objects equal only when they are the same object.

Henry
 
dileep keely
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yep henry cleared..
Correct.I must have used the to check.
 
dileep keely
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A last doubt..


In both the cases above, I believe the hashCode() of Object class is called,I am right isn't it?. If that is the case I should return null ..

 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dileep keely wrote:
I must have used the to check.



which would not serve any purpose, as there is no way any newly instantiate object would be the same exact object as a previously created one.

Henry
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dileep keely wrote:
In both the cases above, I believe the hashCode() of Object class is called,I am right isn't it?. If that is the case I should return null ..



In the first case, the hashCode() method of the two Test objects will be called. And in the second case, the one created Test object's hashCode() method will be called twice.

Henry
 
We can walk to school together. And we can both read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic