Auto Boxing and Unboxing in Java

This post explains the Auto Boxing and Unboxing concept in Java

In boxing primitives converted into appropriate wrapper class (which is Integer in the case of int). And in unboxing reverse is happened.

Definition from Java Docs

As any Java programmer knows, you can’t put an int (or other primitive value) into a collection. Collections can only hold object references, so you have to box primitive values into the appropriate wrapper class (which is inInteger the case of int). When you take the object out of the collection, you get thatInteger put in; if you need an int, you mustunbox the Integer using the intValue method. All of this boxing and unboxing is a pain, and clutters up your code. The autoboxing and unboxing feature automates the process, eliminating the pain and the clutter.

—VectorAutoBoxing—

import java.util.Vector;

public class VectorAutoBoxing {
    public static void main(String args[])
    {
    Vector vector=new Vector();
    vector.add(1);                      //auto boxing
    vector.add(2);                      //auto boxing
    System.out.println(vector);         //auto unboxing
    }
}
//In boxing primitives converted into appropriate wrapper class (which is Integer in the case of int). And in unboxing reverse is happen.

OUTPUT:
unboxing