*
The moose likes Beginning Java and the fly likes Autoboxing & Generics Caveat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Autoboxing & Generics Caveat" Watch "Autoboxing & Generics Caveat" New topic
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: 6109
    
    6



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: 6109
    
    6

You're quite welcome. Glad I could help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Autoboxing & Generics Caveat
 
Similar Threads
Method Hash
long in a byte array
please explain this
Mail API content type
Mail API content type