What is Autoboxing and Unboxing in Java?

Converting a primitive value into an object of the corresponding wrapper class is called autoboxing.

Converting an object of a wrapper class to its corresponding primitive value is called unboxing.


// Java program to illustrate the concept of Autoboxing and Unboxing
public class AutoboxingUnboxingDemo {

	public static void main(String[] args) {

		// Creating an Integer object with value 25
		Integer x = new Integer(25);

		// Unboxing the object
		int y = x;

		// Autoboxing of char
		Character ch1 = 'A';

		// Unboxing of char
		char ch2 = ch1;

		System.out.println("Value of x: " + x);
		System.out.println("Value of y: " + y);
		System.out.println("Value of ch1: " + ch1);
		System.out.println("Value of ch2: " + ch2);



Author: Mahesh

Technical Lead with 10 plus years of experience in developing web applications using Java/J2EE and web technologies. Strong in design and integration problem solving skills. Ability to learn, unlearn and relearn with strong written and verbal communications.