In Java 1.5, Java community has introduced two more new concepts that are Autoboxing and Unboxing in Java. Because of autoboxing and unboxing, we can able to convert primitive datatype to corresponding Java wrapper classes and vice versa.
If you have used collections like ArrayList, hashmap before Java 1.5 version, that time, you may face a problem that 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 object after that only we can use them into the collections. For such scenario wrapper class like integer, double and boolean helps convert the primitive to object.
But in Java 1.5, there is some new feature introduced likes Autoboxing and Unboxing, which helps in converting the primitive datatype to 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:
- What is Autoboxing and Unboxing in Java?
- When Autoboxing and Unboxing occurs?
Autoboxing in Java
Converting a primitive data type to object type of the corresponding wrapper class is called Autoboxing.
Example: Converting of 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 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 expecting 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 Compiler converts the primitive data type to wrapper class object, that time compiler calls valueof() method, in the same when compiler convert wrapper class object to a primitive value that time compiler calls intValue() and doubleValue() methods During autoboxing when we are writing
Integer num=100;
Then Java compiler internally calls the valueOf() to convert the primitive data type to 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 equals() method, and while comparing with the primitive data type, try to use logical operators like “==,” “<” and “>” etc.
Ref: article
Leave a Reply