Autoboxing and Unboxing in Java
Autoboxing converts primitive data types into their equivalent wrapper type, and unboxing is the conversion of wrapper types to primitive data types. This feature is built-in in Java.
This tutorial describes and demonstrates the use of auto-boxing and unboxing in Java.
Auto-boxing in Java
As mentioned above, auto-boxing converts primitive data types into the same wrapper type class as an object. For example, long
to Long
or int
to Integer
.
The list of these primitive data types and their corresponding wrapper type class is below.
Primitive Type | Wrapper Type |
---|---|
int |
Integer |
boolean |
Boolean |
double |
Double |
float |
Float |
byte |
Byte |
char |
Character |
long |
Long |
short |
Short |
There are three cases when we can use auto-boxing in Java:
- We must pass a primitive data type as a parameter in any method. That method requires a corresponding wrapper class object.
- While assigning a primitive data type to a variable with a corresponding wrapper class.
- While working with collection framework classes, the compiler has to perform autoboxing in Java.
There are three cases. Let’s try to create an example for each case. See the example for the first case:
package Delfstack;
public class Autoboxing_Test {
public static void Number(Integer x) {
System.out.println(x);
}
public static void main(String[] args) {
// Compiler performs Autoboxing here. it converts primitive type to wrapper type Integer object.
int x = 10;
Number(x);
}
}
We passed a primitive type int
as a parameter into a method that requires wrapper class Integer
; the compiler will perform autoboxing here. See output:
10
See the example for the second case:
package Delfstack;
public class Autoboxing_Test {
public static void main(String[] args) {
// Assigning primitive long value to Wrapper Long variable x. Compiler performs autoboxing
Long x = 430L;
// Assigning primitive char value to Wrapper Character variable y. Compiler performs autoboxing
Character y = 'a';
System.out.println(x);
System.out.println(y);
}
}
We assigned primitive data type values to the wrapper type variable, and the compiler performed autoboxing. See output:
430
a
See the example for the third case:
package Delfstack;
import java.util.ArrayList;
public class Autoboxing_Test {
public static void main(String[] args) {
ArrayList<Long> Long_List = new ArrayList<Long>();
// while working with collections framework the compiler performs autoboxing
Long_List.add(220L);
Long_List.add(330L);
System.out.println(Long_List);
}
}
While working with collection framework classes, the compiler has to perform autoboxing in Java like in the above code; we add primitive type long
elements to wrapper type Long
list. See output:
[220, 330]
Unboxing in Java
The unboxing in Java converts an object of a wrapper class to the same primitive data type; it is the opposite operation of autoboxing. For example, converting Integer
to int
or Long
to long
, the table earlier can also be used for boxing and vice versa.
The unboxing can also be applied in three cases:
- While passing an object of a wrapper class to a method that requires a value with primitive type.
- When the wrapper class object is assigned to the corresponding primitive variable.
- While working with collection frameworks.
Let’s try an example for each case in Java. See the example for the first case:
package Delfstack;
public class Unboxing_Test {
public static void Number(int x) {
System.out.println(x);
}
public static void main(String[] args) {
Integer x = new Integer(340);
// We passed Integer wrapper class object, the compiler will convert it to int because method
// requires int
Number(x);
}
}
The compiler will perform unboxing while passing an object of a wrapper class to a method that requires a value with a primitive type. See output:
340
The example for the second case:
package Delfstack;
public class Unboxing_Test {
public static void main(String[] args) {
Long x = new Long(340L);
Integer y = new Integer(450);
// Performing unboxing here. assigning Long object to primitive long type similarly Integer to
// int
long a = x;
int b = y;
System.out.println(a);
System.out.println(b);
}
}
The compiler will perform unboxing when the wrapper class object is assigned to the corresponding primitive variable. See output:
340
450
The example for the third case:
package Delfstack;
import java.util.ArrayList;
public class Unboxing_Test {
public static void main(String[] args) {
ArrayList<Long> Long_List = new ArrayList<Long>();
Long_List.add(230L);
Long_List.add(340L);
Long_List.add(450L);
// The get method returns an Long object and we assigned it to the long primitive type
long x = (long) Long_List.get(0);
long y = (long) Long_List.get(1);
long z = (long) Long_List.get(2);
System.out.println(x);
System.out.println(y);
System.out.println(z);
}
}
While working with collection frameworks like ArrayList
, the compiler performs unboxing when we try to assign ArrayList
list elements to primitive type variables. See output:
230
340
450
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook