In Java 1.5, the Java community introduced two more new concepts in Java: autoboxing and unboxing. Because of autoboxing and unboxing, we can convert primitive datatypes to corresponding Java wrapper classes and vice versa.
If you have used collections like ArrayList hashmap before the Java 1.5 version, you may face a problem: you can not put the primitive types into the collections. To use the primitive datatype in the collection, first, we need to convert them to objects; after that, only we can use them in the collections. For such a scenario, a wrapper class like integer, double, and boolean helps convert the primitive to an object.
But in Java 1.5, some new feature is introduced, like Autoboxing and Unboxing, which helps convert the primitive datatype to an object automatically by the Java compiler because of this feature. The code will be more readable.
Both Autoboxing and Unboxing come with some warning, so we need to understand them very clearly before using them in the program because those things happen automatically, and if you do not implement them properly, then that may create bugs.
So for a better understanding of Autoboxing and Unboxing, in this article, we are going to discuss the following:
- What are Autoboxing and Unboxing in Java?
- When do Autoboxing and Unboxing occur?
Autoboxing in Java
Converting a primitive data type to the object type of the corresponding wrapper class is called Autoboxing.
Example: Converting int to an integer, long to Long, etc.
Java Compiler applies autoboxing when a primitive value is passed to a method, but the method is expected to be a wrapper class object.
class AutoboxingExample1 { public static void myMethod(Integer num) { System.out.println(num); } public static void main(String[] args) { /* passed int (primitive type), it would be * converted to Integer object at Runtime */ myMethod(2); } }
When you assign a primitive data type value to a wrapper class object?
class Test { public static void main(String[] args) { Integer iob = 100; //Auto-boxing of int i.e converting primitive data type int to a Wrapper class Integer int i = iob; //Auto-unboxing of Integer i.e converting Wrapper class Integer to a primitve type int System.out.println(i+" "+iob); Character cob = 'a'; //Auto-boxing of char i.e converting primitive data type char to a Wrapper class Character char ch = cob; //Auto-unboxing of Character i.e converting Wrapper class Character to a primitive type char System.out.println(cob+" "+ch); } }
Unboxing In Java
Converting a wrapper class object to a primitive data type is called unboxing. For example, Integer to int. Java compiler applies unboxing when
When a passed a wrapper class object to a method but that method expects a primitive data type value
class UnboxingExample1 { public static void myMethod(int num) { System.out.println(num); } public static void main(String[] args) { Integer inum = new Integer(100); /* passed Integer wrapper class object, it * would be converted to int primitive type * at Runtime */ myMethod(inum); } }
When are you trying to assign a wrapper class object to a primitive data type variable?
ArrayList ArrayList = new ArrayList() int num = ArrayList.get(0); // unboxing because get method returns an Integer object
Primitive type | Wrapper class |
---|---|
boolean | Boolean |
byte | Byte |
char | Character |
float | Float |
int | Integer |
long | Long |
short | Short |
double | Double |
Important point About Autoboxing and Unboxing In Java
When the Compiler converts the primitive data type to a wrapper class object, the time the compiler calls valueof() method, in the same when the compiler converts the wrapper class object to a primitive value that time the compiler calls intValue() and doubleValue() methods During autoboxing when we are writing.
Integer num=100;
Then, the Java compiler internally calls the valueOf() to convert the primitive data type to a wrapper class object. so internally this statement is executed
Integer num=Integer.valueOf(100);
Similarly, at the time of unboxing, when we are trying to convert wrapper class object to the primitive data type for example
Integer num2 = new Integer(50); int inum = num2;
then Java compiler internally calls intValue() method like below
Integer num2 = new Integer(50); int inum = num2.intValue();
Similarly, for other primitive data types, such things happen internally.
Important Point to Keep in Mind During Comparison:
When you are comparing that time, don’t mix primitive data type with an object; if you do so, then you may get an unpredictable result. Better at the time of comparing objects with objects, try to use the equals() method, and while comparing with the primitive data type, try to use logical operators like “==,” “<” and “>” etc.