• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interchanging Integer and int

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link. That helped a lot.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic