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

Interchanging Integer and int

 
Daniel Loranz
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
Daniel Loranz
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link. That helped a lot.
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic