Autoboxing and Unboxing in Java with Examples

In this article, we will discuss what is auto-boxing and unboxing with examples.
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. The reverse i.e Unwrapping the object into corresponding primitive data is called Unboxing.

Autoboxing

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on.

Autoboxing Example

This program demonstrates the auto-boxing of all the primitive types.
/**
 * Class to demonstrate auto-boxing the primitive types.
 * @author javaguides.net
 *
 */
public class AutoBoxingExample {
 public static void main(String[] args) {
  
  byte b = 100; //Primitive byte data
  Byte B = b; //Auto-Boxing of byte data
  System.out.println(B);
  
  short s = 100; //Primitive short data
  Short S = s;   //Auto-Boxing of short data
  System.out.println(S);
  
  int i = 200;    //Primitive int Data
  Integer I = i;  //Auto-Boxing of int data
  System.out.println(I);
  
  long l = 250; 
  Long L = l;
  System.out.println(L);

  float f = 120L; 
  Float F = f;
  System.out.println(F);

  double d = 18.58; 
  Double D = d;
  System.out.println(D);

  boolean bln = false; 
  Boolean BLN = bln;
  System.out.println(BLN);

  char c = 'C'; 
  Character C = c;
  System.out.println(C);
 }
}
Output:
100
100
200
250
120.0
18.58
false
C
From JDK 1.5 onwards, Auto-Unboxing is introduced. We can explicitly box the primitive types by creating their corresponding wrapper class. Below section explained this boxing concept.

Boxing

we can explicitly box the primitive types by creating their corresponding wrapper class.
Example: This example demonstrates what is boxing.
/**
 * Class to demonstrate boxing of primitive types.
 * @author javaguides.net
 *
 */

public class BoxingExample {
 public static void main(String[] args) {
  
  byte b = 100;
  Byte B = new Byte(b); 
  System.out.println(B);
  
  short s = 100; 
  Short S = new Short(s); 
  System.out.println(S);
  
  int i = 200; 
  Integer I = new Integer(i);
  System.out.println(I);
  
  long l = 250; 
  Long L = new Long(l); 
  System.out.println(L);

  float f = 120L; 
  Float F = new Float(f);
  System.out.println(F);

  double d = 18.58; 
  Double D = new Double(d);
  System.out.println(D);

  boolean bln = false; 
  Boolean BLN = new Boolean(bln);
  System.out.println(BLN);

  char c = 'C'; 
  Character C = new Character(c); 
  System.out.println(C);
 }
}
Output:
100
100
200
250
120.0
18.58
false
C

Auto-Unboxing

Compiler implicitly converts wrapper object to corresponding primitive data if you assign wrapper object to a primitive type variable.

Auto-Unboxing Example

Example: This program demonstrates the usage of Auto-unboxing in Java.
/**
 * Class to demonstrate auto-unboxing the wrapper object.
 * @author javaguides.net
 *
 */
public class AutoUnboxingExample {
 public static void main(String[] args) {
  Byte B = new Byte((byte) 10);   //Byte Object
        byte b = B;                    //Auto-Unboxing of Byte Object
        System.out.println(b);
        
        Short S = new Short((short) 20);   //Short Object
        short s = S;                      //Auto-Unboxing of Short Object
        System.out.println(s);
        
        Integer I = new Integer(15);    //Integer Object
        int i = I;                     //Auto-Unboxing of Integer Object
        System.out.println(i);
        
        Long L = new Long(50);     //Long Object
        long l = L;               //Auto-Unboxing of Long Object
        System.out.println(l);
        
        Float F = new Float(20);      //Float Object
        float f = F;                 //Auto-Unboxing of Float Object
        System.out.println(f);
        
        Double D = new Double(20.5);   //Double Object
        double d = D;                 //Auto-Unboxing of Double Object
        System.out.println(d);
        
        Boolean BLN = new Boolean(true);      //Boolean Object
        boolean bln = BLN;                   //Auto-Unboxing of Boolean Object
        System.out.println(bln);
        
        Character C = new Character('C');    //Character Object
        char c = C;                         //Auto-Unboxing of Character Object
        System.out.println(c);
 }
}
Output:
10
20
15
50
20.0
20.5
true
C
From JDK 1.5 onwards, Auto-Unboxing is introduced. We can explicitly convert wrapper object to corresponding primitive data type. below section explained this Unboxing concept.

Unboxing

we can explicitly convert wrapper object to corresponding primitive data type using wrapper class object methods such as byteValue(), shortValue(), longValue() etc.
Example: This example demonstrates what is Unboxing.
/**
 * Class to demonstrate unboxing the wrapper object.
 * call boolean method to unbox the wrapper object
 * @author javaguides.net
 *
 */

public class UnboxingExample {
 public static void main(String[] args) {
  Byte B = new Byte((byte) 10); // Byte Object
  byte b = B.byteValue(); // Unwrapping Byte object to byte data
  System.out.println(b);

  Short S = new Short((short) 20); // Short Object
  short s = S.shortValue(); // Unwrapping Short object to short data
  System.out.println(s);

  Integer I = new Integer(15); // Integer Object
  int i = I.intValue(); // Unwrapping Integer object to int data
  System.out.println(i);

  Long L = new Long(50); // Long Object
  long l = L.longValue(); // Unwrapping Long object to long data
  System.out.println(l);

  Float F = new Float(20); // Float Object
  float f = F.floatValue(); // Unwrapping Float object to float data
  System.out.println(f);

  Double D = new Double(20.5); // Double Object
  double d = D.doubleValue(); // Unwrapping Double object to double data
  System.out.println(d);

  Boolean BLN = new Boolean(true); // Boolean Object
  boolean bln = BLN.booleanValue(); // Unwrapping Boolean object to
  System.out.println(bln); // boolean data

  Character C = new Character('C'); // Character Object
  char c = C.charValue(); // Unwrapping Character object to char data
  System.out.println(c);
 }
}
Output:
10
20
15
50
20.0
20.5
true
C

Wrapper Classes Articles

Comments