«Back to Home

Core Java

Topics

Autoboxing And Autounboxing In Java

Autoboxing and Autounboxing
 
In Java, autoboxing and autounboxing is the new feature of Java5. The automatic conversion of primitive data types into its equivalent wrapper type is called as autoboxing and the automatic conversion of wrapper class type into the corresponding primitive type means an opposite operation is called as autounboxing. Thus, programmer doesn't need to write the conversion code. There is no need of conversion between primitives and wrappers manually.
 
Let’s see an example of autoboxing, given below.
 
Code
  1. public class AutoBoxingExample {  
  2.     public static void main(String args[]) {  
  3.         int x = 78;  
  4.         Integer y = new Integer(x);  
  5.         Integer z = 59;  
  6.         System.out.println(y + " " + z);  
  7.     }  
  8. }  
31

Output

32

Let’s see an example of autounboxing, given below.
 
Code
  1. public class AutoUnboxingExample {  
  2.     public static void main(String args[]) {  
  3.         Integer i = new Integer(536);  
  4.         int x = i;  
  5.         System.out.println(x);  
  6.     }  
  7. }  
33

Output

34

Autoboxing and Autounboxing with comparison operators

Let’s see an example, given below.
 
Code
  1. public class AutoUnboxingExample {  
  2.     public static void main(String args[]) {  
  3.         Integer i = new Integer(53);  
  4.         if (i < 100) {  
  5.             System.out.println(i);  
  6.         }  
  7.     }  
  8. }  
35

Output

36
 
Autoboxing and Autounboxing with method overloading

Autoboxing and autounboxing can be performed in method overloading but there are some rules for method overloading with autoboxing, which are,
  • Widening beats boxing

  • Widening beats varargs

  • Boxing beats varargs
Let’s see an example of autoboxing, where widening beats boxing, given below.
 
Code
  1. public class AutoBoxingExample {  
  2.     static void display(int i) {  
  3.         System.out.println("int");  
  4.     }  
  5.     static void display(Integer i) {  
  6.         System.out.println("Integer");  
  7.     }  
  8.     public static void main(String args[]) {  
  9.         short s = 96;  
  10.         display(s);  
  11.     }  
  12. }  
37

Output

38

Let’s see an example of autoboxing, where widening beats varargs are given below.
 
Code
  1. public class AutoBoxingExample {  
  2.     static void display(int i, int i2) {  
  3.         System.out.println("int int");  
  4.     }  
  5.     static void display(Integer...i) {  
  6.         System.out.println("Integer Integer");  
  7.     }  
  8.   
  9.     public static void main(String args[]) {  
  10.         short a = 30, b = 40;  
  11.         display(a, b);  
  12.     }  
  13. }  
39

Output

40

Let’s see an example of autoboxing, where boxing beats varargs are given below.
 
Code
  1. public class AutoBoxingExample {  
  2.     static void display(Integer i) {  
  3.         System.out.println("Integer");  
  4.     }  
  5.     static void display(Integer...i) {  
  6.         System.out.println("Integer...");  
  7.     }  
  8.   
  9.     public static void main(String args[]) {  
  10.         int x = 654;  
  11.         display(x);  
  12.     }  
  13. }  
41

Output

42

Let’s see an example of method overloading with widening and boxing, given below.
 
Code
  1. public class AutoBoxingExample {  
  2.     static void display(Long l) {  
  3.         System.out.println("Long");  
  4.     }  
  5.     public static void main(String args[]) {  
  6.         int x = 564;  
  7.         display(x); // compile time error  
  8.     }  
  9. }  
43

Summary

Thus, we learnt that the automatic conversion of the primitive data types into its equivalent wrapper type is called as autoboxing and opposite operation is called as autounboxing and also learnt how to use it in Java.