Oct 122016

ADM

Autoboxing and Unboxing in Java

  • 12 October 2016
  • ADM

 

 

 

Autoboxing and Unboxing in Java - images/logos/java.jpg

 

In Java, for each primitive data type, there is a corresponding wrapper class.

Since Java 1.5 was introduces two automatic conversions between the primitive type and the corresponding wrapper class. These automatic conversions are known as:

  • 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.
  • Unboxing is the opposite conversion. For example conversion of an Integer Object to a primitive int.

Here is the list of all the primitive types the corresponding wrapper classes:

Primitive type Wrapper class
boolean Boolean
byte Byte
char Character
float Float
int Integer
long Long
short Short
double Double

Autoboxing

The Java compiler applies autoboxing when a primitive value is:

  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.

Example

package com.admfactory;

public class AutoBoxingExample {
    
    /**
     * 
     *	Returns the sum of the parameters
     * @param a
     * @param b
     * @return a + b
     */
    public static Integer add(Integer a, Integer b) {
	return a + b;
    }
    
    public static void main(String[] args) {
	
	int x0 = 100;
	
	/** Boxing */
	Integer x1 = new Integer(x0);
	
	/** Boxing done automatically */
	Integer x2 = x0;
	
	/** Boxing - the method parameters */
	Integer x3 = add(10, 12);
	
	System.out.println("Boxing Example");
	System.out.println("x0: " + x0);
	System.out.println("x1: " + x1);
	System.out.println("x2: " + x2);
	System.out.println("x3: " + x3);
    }
}

The Java compiler will convert all primitive int to Integer object at runtime. Is like instead of

Integer x3 = add(10, 12);

you write

Integer x3 = add(Integer.valueOf(10), Integer.valueOf(12));

Output

Boxing Example
x0: 100
x1: 100
x2: 100
x3: 22

Unboxing

The Java compiler applies unboxing when a wrapper object is:

  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

Example

package com.admfactory;

public class UnBoxingExample {

    /**
     * 
     * Returns the sum of the parameters
     * 
     * @param a
     * @param b
     * @return a + b
     */
    public static int add(int a, int b) {
	return a + b;
    }

    public static void main(String[] args) {

	Integer x0 = new Integer(100);

	/** Unboxing */
	int x1 = x0;

	/** Unboxing - the method parameters */
	int x2 = add(x0, x0);

	System.out.println("Unboxing Example");
	System.out.println("x0: " + x0);
	System.out.println("x1: " + x1);
	System.out.println("x2: " + x2);

	/** Unboxing internally */
	if (x0 == 100) {
	    System.out.println("x0 = 100");
	}
    }
}

At first look I would say that x0 == 100 will cause an error, but the compiler does not generate an error because it invokes the intValue method to convert an Integer to an int at runtime:

if (x0.intValue() == 100) {
    System.out.println("x0 = 100");
}

Output

Unboxing Example
x0: 100
x1: 100
x2: 200
x0 = 100

 

 

 

References