Autoboxing And Unboxing In Java

Introduction

 
In this article, we discuss Autoboxing and Unboxing in Java as a Java5 new feature.
 

Java Autoboxing and Unboxing

 
Conversion of primitive data types into their equivalent Wrapper type automatically is known as Autoboxing and the reverse operation of that is known as Unboxing. This is a new feature of Java5. So the Java programmer doesn't need to write the conversion code.
 

Advantages

 
There is no need for manual conversion so we have less coding to do.
 
Example 1: This example shows Autoboxing in Java.
  1. class AutoBoxingEx1 {  
  2.  public static void main(String args[]) {  
  3.   int x = 100;  
  4.   Integer x2 = new Integer(x);  
  5.   Integer x3 = 5;  
  6.   System.out.println(x2 + " " + x3);  
  7.  }  
  8. }   
Output
 
Fig-1.jpg
 

Using comparison operators to define Autoboxing and Unboxing

 
Using comparison operators Autoboxing is performed.
 
Example 2: In this example; using comparison operators we saw Autoboxing and Unboxing.
  1. class UnBoxingEx1 {  
  2.  public static void main(String args[]) {  
  3.   Integer x = new Integer(150);  
  4.   if (x < 200) {  
  5.    System.out.println(x);  
  6.   }  
  7.  }  
  8. }   
Output
 
Fig-2.jpg
 

Using method overloading define Autoboxing and Unboxing

 
There are some rules for method overloading with boxing.
  • widening beats varargs
  • widening beats boxing
  • Boxing beats varargs

1. Autoboxing where widening beats varargs

 
If there is the possibility of widening and varargs in Java then widening beats var-args.
 
Example
  1. class AutoboxingEx3 {  
  2.  static void mthd(int x, int x2) {  
  3.   System.out.println("display int display int");  
  4.  }  
  5.  static void mthd(Integer...x) {  
  6.   System.out.println("display Integer...");  
  7.  }  
  8.  public static void main(String args[]) {  
  9.   short srt1 = 120, srt2 = 140;  
  10.   mthd(srt1, srt2);  
  11.  }  
  12. }   
Output
 
Fig-4.jpg
 

2. Autoboxing where widening beats boxing

 
The following example shows the following Autoboxing where widening beats boxing.
 
Example
  1. class AutoboxingEx2 {  
  2.  static void mthd(int x) {  
  3.   System.out.println("display int");  
  4.  }  
  5.  static void mthd(Integer x) {  
  6.   System.out.println("display Integer");  
  7.  }  
  8.  public static void main(String args[]) {  
  9.   short srt = 120;  
  10.   mthd(srt);  
  11.  }  
  12. }  
Output
 
Fig-3.jpg
 

3. Autoboxing where boxing beats varargs

 
The following example shows that boxing beats a variable argument.
 
Example
  1. class AutoboxingEx4 {  
  2.  static void mthd(Integer x) {  
  3.   System.out.println("Display Integer");  
  4.  }  
  5.  static void mthd(Integer...x) {  
  6.   System.out.println("Display Integer...");  
  7.  }  
  8.  public static void main(String args[]) {  
  9.   int k = 130;  
  10.   mthd(k);  
  11.  }  
  12. }   
Output
 
Fig-5.jpg
 

Method overloading using Boxing and Widening.

 
The following example shows that Widening and Boxing can't be performed.
 
Example
  1. class AutoboxingEx5 {  
  2.  static void mthd(Long lng) {  
  3.   System.out.println("Display Long");  
  4.  }  
  5.  public static void main(String args[]) {  
  6.   int x = 120;  
  7.   mthd(x);  
  8.  }  
  9. }   
Output:
.
Fig-6.jpg
 
Note: Since they generate a compile-time error that shows we can't perform Widening and Boxing at a time