Autoboxing and Unboxing

Wrapper classes offer various methods to convert value of object into their corresponding primitive value. for example int intValue() method allow programmer to convert wrapper class object into int data type.

Important Autoboxing is automatic conversion of primitive data type into their wrapper class type and reverse of it known as Unboxing.


Example


 class con
{
public static void main(String ...arg)
{
Integer a=12; // automatic conversion of primitive value to object: Autoboxing.
Integer b=new Integer(20);
int x=b; // Automatic conversion from objec to primitive type : Unboxing

}
}

In above example we haven't used any "new" keyword to create object of Integer class. Java automatically has created object with value 12. This process also known as autoboxing . In next instruction we have created a Integer object which we have assigned to variable x. Here objec value is extracted and assigned to variable x.



Autoboxing and Unboxing happen for two reasons

  • When primitive value passed in method requires wrapper class object or primitive type
  • When primitive value assigned to wrapper class variable or reverse


Advantage of autoboxing and unboxing

  • No need to make object explicitly and wrap the value of primitive type.

  • Autoboxing/Unboxing in Methods

    Autoboxing automatically occur when primitive type must be converted into an object.

    Example

    
      class con 
    {
    static int value(Integer a)
    {
    return a;
    }
    public static void main(String arg[])
    {
    int b=10;
    int c= value(b); // Automatically b boxed into Integer object.
    }
    }

    In above example method value is taking one argument of Integer class object, and return that object. But return data type of method value is int so compiler automatically extract value from object(Unboxed) and return int type value.

    In main method we have passed variable b in method value. Type of b variable is int so compiler automatically box this variable's value into a object of Integer class and pass it in the method.



    Autoboxing/Unboxing in Expression



    In expression if there is any wrapper class object is used, it's automatically gets unboxed.

    Example

    
      class exp 
    {
    public static void main(String ...arg)
    {
    Integer a=12; // Autoboxing
    Integer b;

    b=a+5; // a gets unboxed, expression evaluated and result is boxed in object.
    int x=b+7; // b unboxed, expression evaluated and result assigned to x
    }
    }

    In above example "b=a+5;" instruction involve two object of Integer class. Compiler automatically extract value of object a(Unboxing) and evaluate expression. Result boxed in an object and assigned to b(autoboxing).



    Cached Object

    This is a important concept in java while studying Autoboxing and Unboxing. Java cache Integer object in the range of -128 to 127. It means if two different object having value less in range -128 to 127 then both object's reference variable point to same object. This happen during autoboxing and unboxing.

    Example

    
    class con 
    {
    public static void main(String arg[])
    {
    Integer a=120; // Autoboxing
    Integer b=120; // Autoboxing --> During autoboxing java cache Integer in range -128 to 127
    System.out.println("a==b:"+(a==b));
    a=150; // Here java won't cache these Integers, so both having different objects.
    b=150;
    System.out.println("a==b:"+(a==b));
    }
    }
    /*Output:
    a==b:true
    a==b:false
    */


    Widening, Autoboxing and Var-args

    These three concepts always make a confusion for students.

    • Primitive Widening > Boxing > Var-args

      for example just see example given below

      
       public class Tester {
      static void call(Long x, Long y)
      {
      System.out.print("Long x, Long y");
      }

      static void call(int... x)
      {
      System.out.print("int... x");
      }

      static void call(Number x, Number y)
      {
      System.out.print("Number x, Number y");
      }

      public static void main(String[] args) {
      int val = 3;
      call(val, val);
      }
      }

      Among three var-args is last choice. Now we are left with first and third method. int primitive data type can't be converted to directly into Long. It means widening can be done after autoboxing. So int will be autoboxed to Integer and then it is widened to Number.


      Output is "Number x, Number y"

    • Boxing is preferred over var-args.

      
        public class methodOverloading{
      public static void hello(Integer x){
      System.out.println("Autoboxing");
      }

      public static void hello(int ...x){
      System.out.println("var arg");
      }

      public static void main(String[] args){
      int i = 5;
      hello(i);
      }
      }
      /*Output:
      Autoboxing
      */

      Important ! Autoboxing is preferred over var-args.


    • Widening among wrapper classes not allowed


      
        public class methodOverloading{
      public static void hello(Integer x){
      System.out.println("Integer");
      }

      public static void hello(Long l){
      System.out.println("Long");
      }

      public static void main(String[] args){
      Short t=3.2;
      hello(t);
      }
      }