Home >Java >javaTutorial >Autoboxing and Unboxing in Java
Autoboxing is Java compiler’s automatic conversion between the primitive types and their corresponding wrapper class objects, i.e., conversion from int to Integer, double to Double, etc. Unboxing is the automatic conversion from wrapper class objects to their equivalent primitives, i.e., Integer to int. The feature was introduced in version 1.5 of Java.
The compiler uses valueOf() method to convert primitives to corresponding wrapper objects (i.e. autoboxing) internally, and in the vice versa case, it uses intValue(), doubleValue(), etc., like paradigms for unboxing.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
The primitive type and wrapper class mapping in java are as follows:
Primitive type | Wrapper class |
boolean | Boolean |
byte | Byte |
char | Character |
float | Float |
int | Integer |
long | Long |
short | Short |
double | Double |
Let’s take an ArrayList of Integers and make use of the unboxing concept.
import java.util.ArrayList; public class MyClass { public static void main(String args[]) <em>{</em> ArrayList<Integer> intlist = new ArrayList<Integer>(); //wrapper Integer objects being added here intlist.add(1); interest.add(2); //auto-unboxing is happening here int x = intlist.get(0); System.out.println(x); } }
Hence, in the above example, while adding value to x, we see that x appears to be primitive. As a result, unboxing happens here automatically while the assignment is done.
public class MyClass { public static void main(String args[]) { Integer sum =0; for(int i=0;i<10;i++) { sum = sum + i; } System.out.println(sum); } }
Consider the snippet placed below; what will be the output of this?
public class Main { public static void main(String[] args) { Integer m = 34123; Integer x = 34123; System.out.println(x==m); } }
public class Main { public static void main(String[] args) { Integer m = 100; Integer x = 100; System.out.println(x==m); } }
This will evaluate to “true” value, as 100 is present in the literal pool.
public class Main { public static void main(String[] args) { Overload obj = new Overload(); int i =5; obj.printval(5); Integer m = i; obj.printval(m); } } class Overload { public void printval(int i) { System.out.println("printing the unboxed value "+ i); } public void printval(Integer i) { System.out.println("printing the autoboxed value "+ i); } }
Output:
Note: You can execute the above program in any of the IDEs to get the above output.We saw the use case of autoboxing and unboxing and how implicit this concept is, along with its pros and cons. It must be used with caution when coding; otherwise, it can add unnecessary computational conversion overhead. Consequently, conversions must be done in primitives to avoid excessive garbage collection overhead and temporary object creation. We also saw the use case of autoboxing with the overload concept in Java. You can check a few more constraints along with this.
The above is the detailed content of Autoboxing and Unboxing in Java. For more information, please follow other related articles on the PHP Chinese website!