• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is Autoboxing and unboxing in java  RSS feed

 
Danny Alphones
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble understanding Autoboxing and unboxing in java if you could explain it to me the easiest way possible that would be great thank you.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Danny,
First think about why these are valid:


Simple, right? That's because you are assigning the value on the right to the type on the left. Then consider:


Still good. Same type on the right assigned to the variable on the left. Now how about?


Here we have and int primitive being assigned to the Integer type. They aren't the same type, but it works because of boxing.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most things (but not all) are objects in Java.  The few that aren't are primitives like int and double.  It turns out there are times that an object is needed for a primitive.  So classes like Integer and Double were created.  Very simply, autoboxing happens when you have a primitive, but needs an object.  Unboxing happens when you need a primitive and you have an object. 

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oracle's Java Tutorials also contains a page on autoboxing: Autoboxing and Unboxing
 
Danny Alphones
Ranch Hand
Posts: 52
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!