• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Interchanging Integer and int

 
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!
 
Ranch Hand
Posts: 2908
1
Spring Java 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.
 
Sheriff
Posts: 9099
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.
 
Seriously? That's what you're going with? I prefer this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic