| Author |
Autoboxing & Generics Caveat
|
Ustad Hanif
Greenhorn
Joined: Oct 18, 2010
Posts: 5
|
|
Could someone kindly tell me why the following piece of code behaves the way it does? Basically I've defined a hashmap where the key is a Long object. I populate the map with an entry and then try and retrieve that entry with an int primitive. I understand the int I pass into get() is autoboxed into an Integer. The compile-time check asserts that myint can be promoted from int to long, but what is happening at run-time that causes a value of 'null' to be returned? I prostrate before the Ranch and beseech its apt members for guidance!!
results:
my value for int key is: null
my value for long key is: hello
|
"Wait till you look within yourself and see what is there.... Oh Seeker, one leaf in that garden is worth more than all of Paradise!"
Rumi
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5893
|
|
That last line of code is what the Map's key matching will be doing (if the hashCodes even match in the first place, which they may or may not).
|
 |
Ustad Hanif
Greenhorn
Joined: Oct 18, 2010
Posts: 5
|
|
|
Concise, right to the point and beautifully answered! Thanks also for the lightning speed response time Jeff!
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5893
|
|
You're quite welcome. Glad I could help.
|
 |
 |
|
|
subject: Autoboxing & Generics Caveat
|
|
|