Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode()-> reference,Object

 
dileep keely
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • 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..
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 108
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep henry cleared..
Correct.I must have used the to check.
 
dileep keely
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic