Advertisement



< Prev
Next >



Autoboxing and Unboxing





Prior to Java 5, the only way to convert a specific primitive value to an object of its matching wrapper class, was to call the constructor of a matching wrapper class object with this primitive value.




Boxing before Java 5


class A
{
public static void main(String... ar)
{
//Calling the construtor to box a primitive value in its equivalent wrapper class object.

Integer in= new Integer(10); 		//converting an int to Integer object.
Character c = new Character('h');	//converting a char to Character object.
Float f= new Float("10.2");		//converting a float to Float object.
Double d= new Double("22.2");		//converting a double to Double object.
}
}






Autoboxing - automatic conversion of a primitive value to a wrapper object.


With the version Java 5, Java introduced a new update called Autoboxing. Autoboxing takes place when a primitive value is automatically converted to its matching wrapper class object, by only directly assigning. This operation is called autoboxing, because boxing a primitive value in an wrapper class object is as simple and quick, without having to call a constructor of a matching wrapper class(which was the case prior Java 5)




Note :


Sometimes Autoboxing is also referred as boxing.




Autoboxing example


//Autoboxing a primitive to its equivalent wrapper class object.
class A
{
public static void main(String... ar)
{

Character ch = 'c';  	//Autoboxing a char to Character by assigning
Boolean bool = true; 	//Autoboxing a boolean to Boolean by assigning
Byte byte = 100;	//Autoboxing a byte to Byte by assigning
Short sh =  10;		//Autoboxing a short to Short by assigning
Integer i = 10; 	//an int is boxed into an Integer object.
Long l = 1000L;	//Adding l or L is neccessary because by default every non-decimal numeric value is int.
Float fl = 10.5f;//Adding f or F is neccessary because by default every decimal value is double.
Double db = 25.5;
}
}



Advertisement




Unboxing before Java 5


  • Prior to Java 5, the only way to convert a wrapper class object to a primitive type value was to call method, xxxValue(), where xxx is replaced by the name of a primitive data type in which you want to unbox your wrapper class object. For example -
    • charValue() is a method of Character wrapper class, which unboxes the value of Character object and returns it as a primitive char.
    • intValue() is a method of Integer wrapper class, which unboxes the value of Integer object and returns it as a primitive int.

    Let's us understand the concept with a coding example -
    class A
    {
    public static void main(String... ar)
    {
    //Boxing
    Character ch = new Character('c');
    Boolean bool = new Boolean(true);
    Byte byt = new Byte(100);
    Short sh =  new Short(10);
    Integer intg = new Integer(10);
    Long lg = new Long(1000l);
    Float fl = new Float(10.5f);
    Double db = new Double(25.5);
    
    //Calling ---Value() method of each wrapper class to unbox the object to its primitive value.
    char c= ch.charValue();
    boolean b= bool.booleanValue();
    byte bt = byt.byteValue();
    short s= sh.shortValue();
    int i= intg.intValue();
    long lo= lg.longValue();
    float f= fl.floatValue();
    double d= db.doubleValue();
    
    System.out.println("Unboxed primitive value of Character object : " + c);
    System.out.println("Unboxed primitive value of Boolean object : " + b);
    System.out.println("Unboxed primitive value of Byte object : " + bt);
    System.out.println("Unboxed primitive value of Short object : " + s);
    System.out.println("Unboxed primitive value of Integer object : " + i);
    System.out.println("Unboxed primitive value of long object : " + lo);
    System.out.println("Unboxed primitive value of Float object : " + f);
    System.out.println("Unboxed primitive value of Double object : " + db);
    
    }
    }


    Output -


    Unboxed primitive value of Character object : c
    Unboxed primitive value of Boolean object : true
    Unboxed primitive value of Byte object : 100
    Unboxed primitive value of Short object : 10
    Unboxed primitive value of Integer object : 10
    Unboxed primitive value of long object : 1000
    Unboxed primitive value of Float object : 10.5
    Unboxed primitive value of Double object : 25.5





    Auto-unboxing - automatic conversion of a wrapper object to a primitive value.


  • With Java 5 came the feature of auto-unboxing. Auto-unboxing takes place when an object of a wrapper class is automatically converted to its matching primitive value, by only assigning this wrapper class object reference to to its matching primitive type variable.
    This operation is called auto-unboxing, because unboxing a wrapper class object to its primitive form is as simple and quick as assigning, without having to call any method of a wrapper class(which was the case prior Java 5).




    Note :


    Sometimes Auto-unboxing is also referred as unboxing.




    Unboxing example


    //Autoboxing a primitive to its wrapper class object, directly.
    
    class A
    {
    public static void main(String... ar)
    {
    
    //Autoboxing
    Character ch = 'c';
    Boolean boo = true;
    Byte byt = 100;
    Short sh =  10;
    Integer intg = 10; 
    Long lg = 1000l;
    Float fl = 10.5f;
    Double db = 25.5;
    
    //Auto-unboxing
    char c= ch;		//direct unboxing a Character object to a primitive char.
    boolean b= boo;		//direct unboxing a Boolean object to a primitive boolean.
    byte bt = byt;		//direct unboxing a Byte object to a primitive byte.
    short s= sh;		//direct unboxing a Short object to a primitive short.
    int i= intg;		//direct unboxing a Integer object to a primitive int.
    long lo= lg;		//direct unboxing a Long object to a primitive long.
    float f= fl;		//direct unboxing a Float object to a primitive float.
    double d= db;		//direct unboxing a Double object to a primitive double.
    
    System.out.println("Unboxed primitive value of Character object " + c);
    System.out.println("Unboxed primitive value of Boolean object " + b);
    System.out.println("Unboxed primitive value of Byte object " + bt);
    System.out.println("Unboxed primitive value of Short object " + s);
    System.out.println("Unboxed primitive value of Integer object " + i);
    System.out.println("Unboxed primitive value of long object " + lo);
    System.out.println("Unboxed primitive value of Float object " + f);
    System.out.println("Unboxed primitive value of Double object " + db);
    
    }
    }


    Output-


    Unboxed primitive value of Character object c
    Unboxed primitive value of Boolean object true
    Unboxed primitive value of Byte object 100
    Unboxed primitive value of Short object 10
    Unboxed primitive value of Integer object 10
    Unboxed primitive value of long object 1000
    Unboxed primitive value of Float object 10.5
    Unboxed primitive value of Double object 25.5





    Autoboxing and unboxing when calling a method


    class A
    {
    
    public void boxUnbox(Integer a) //primitive value being passed is autoboxed to an Integer object
    {
    ++a;  //Auto-unbox, Increment and Auto-box
    int c = a; //Auto-unboxing of Integer to int.
    System.out.println(c); 
    }
    
    public static void main(String... ar)
    {
    int a = 10;
    A ob = new A();
    ob.method(a); //passing primitive int value to a method that accepts an Integer object argument.
    }
    }


    Output-


    11
    • In this last code, a primitive int value of 10 is passed to a method boxUnbox() that accepts an Integer object. Hence, this primitive value is automatically boxed to an Integer object.
      • In the method boxUnbox(), pre-increment operator is applied on Integer object that leads to -
      • Auto-unboxing of Integer object to a primitive value(which is not shown).
      • Incremention of thisprimitivevalue.
      • Auto-boxing of primitive value back to the wrapper Integer object, a.
      • Finally, this Integer object is auto-unboxed to an int and its value is displayed in the output.





      Please share this article -






      Advertisement

  • Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON


    Advertisement