aspose file tools*
The moose likes Cattle Drive and the fly likes Interchanging Integer and int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Interchanging Integer and int" Watch "Interchanging Integer and int" New topic
Author

Interchanging Integer and int

Daniel Loranz
Ranch Hand

Joined: Jun 17, 2008
Posts: 41
It looks like Java does some behind the scenes things so I can almost think of Integer and int as being the same - correct?

For example, I have a HashMap<String, Integer> called numberMap.

According to the java doc, when I do numberMap.get(Key), an object is returned. But I can stick the returned value into int no problem.

Is java basically helping me out here by letting me be sloppy with int vs Integer? Or is there something else happening?

Thanks for any helpful comments folks can provide on this.

Thanks!
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by Daniel Loranz:
It looks like Java does some behind the scenes things so I can almost think of Integer and int as being the same - correct?

But I can stick the returned value into int no problem.

Is java basically helping me out here by letting me be sloppy with int vs Integer? Or is there something else happening?



Yes, there is something "behind the scene" job done by java when wrapper classes comes into picture , you can assign Integer object to primitive int OR you can do vise verse !

And that`s called 'autoboxing' and 'autoinboxing' !

and no can explain this better than SUN, itself !

Hope this Help !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Daniel Loranz
Ranch Hand

Joined: Jun 17, 2008
Posts: 41
Thanks for the link. That helped a lot.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Daniel Loranz:
Is java basically "helping" me out here by letting me be sloppy with int vs Integer?

1) It doesn't work in jdk1.4
2) I think there is value in autoboxing... it helps keep your code simple when using Collections... but if you're not careful, you can use up a lot of unnecessary cycles boxing and unboxing where it isn't really necessary or advisable.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interchanging Integer and int