It's not a secret anymore!*
The moose likes Java in General and the fly likes hashCode()-> reference,Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "hashCode()-> reference,Object" Watch "hashCode()-> reference,Object" New topic
Author

hashCode()-> reference,Object

dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
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..
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18544
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
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
Sheriff

Joined: Sep 28, 2004
Posts: 18544
    
  40

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

Joined: Jun 28, 2010
Posts: 91
yep henry cleared..
Correct.I must have used the to check.
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
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
Sheriff

Joined: Sep 28, 2004
Posts: 18544
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18544
    
  40

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
 
jQuery in Action, 2nd edition
 
subject: hashCode()-> reference,Object
 
Similar Threads
same key for diff values in HashMap
[hashCode]two meaningfully equal objects in one bucket
can you please this doubt about hashcode()
about map (HashMap to be specific)
HashMap and hashCode method