AutoBoxing

  • Java 5 supports automatic conversion of primitive types (int, float, double etc.) to their object equivalents (Integer, Float, Double,...) in assignments and method and constructor invocations. This conversion is know as autoboxing.
  • Java 5 also supports automatic unboxing, where wrapper types are automatically converted into their primitive equivalents if needed for assignments or method or constructor invocations.

Example
int i = 0;
i = new Integer(5); // auto-unboxing
Integer i2 = 5; // autoboxing

For Example:

	class T 
{ 
        int d; 
        T(int d) 
        { 
                this.d = d; 
        } 
} 
public class UsingWrapperClass { 

        public static void main(String[] args) { 
        String zzz = null; 
        if(zzz!=null) 
        { 
        zzz.length(); 
        } 
                T obj = new T(1000); 
                Integer i = 1000;  // 1.5  Boxing 
        //Integer i = new Integer(1000);  1.4 or 1.4< 
        Integer s = i; 
        int j = 900; 
        int k = j; 
        int d = i;  // 1.5  UnBoxing 
        int w = i.intValue();  // 1.4 or 1.4 < 
        Integer a = 120; 
        Integer b = 120; 
        if(a==b) 
        { 
                System.out.println("Same Reference"); 
        } 
        else 
        { 
                System.out.println("Not Same Reference"); 
        } 



        } 

}