Share on Google+Share on Google+

Autoboxing unboxing in Java - Java Tutorials

Autoboxing unboxing in Java

Autoboxing and Unboxing

The Autoboxing and Unboxing  was released with the Java 5.


During assignment, the automatic transformation of primitive  type(int, float, double etc.) into their object equivalents or wrapper type(Integer, Float, Double,etc) is known as Autoboxing.

During assignment or calling of constructor, the automatic transformation of wrapper types into their primitive equivalent  is known as Unboxing.

Conversion of int into Integer

int inative = 0;

inative = new Integer(5); // auto-unboxing

Integer intObject = 5; // autoboxing

Storing int a into vector vt

int a = 10;

Vector <integer> vt = new Vector <integer> ();


int n = vt.elementAt(0); </integer></integer>

Autoboxing also works with comparison

int a = 10;

Integer b = 10;


Output of the above code will be :

   Output :               


Boxing/Unboxing of Character value :

public class MainClass {
public static void main(String args[]) {
Boolean booleanObject = true;

if (booleanObject){
System.out.println("b is true");

Character ch = 'x'; // box a char
char ch2 = ch; // unbox a char

System.out.println("ch2 is " + ch2);


  b is true                    
 ch2 is x



Posted on: April 18, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Autoboxing Yourself in JDK 1.5- Java Tutorials  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 2, 2012
wat is the Autoboxing &un boxing

hi i m new for the java ... can u pls tellme wat is the Autoboxing &un boxing.......... and y should and where can we use this concepts..............?
siva kumar
July 30, 2012

Is AUTO BOXING and AUTO UNBOXING will happen only when we are using collections??