Autoboxing and Unboxing:

Autoboxing and Unboxing:

The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn’t need to write the conversion code.

Advantage of Autoboxing and Unboxing:

No need of conversion between primitives and Wrappers manually so less coding is required.

Simple Example of Autoboxing in java:

  1. class BoxingExample1{
  2.   public static void main(String args[]){
  3.     int a=50;
  4.         Integer a2=new Integer(a);//Boxing
  5.         Integer a3=5;//Boxing
  6.         System.out.println(a2+” “+a3);
  7.  }
  8. }

Test it Now

Output:50 5
download this example

Simple Example of Unboxing in java:

The automatic conversion of wrapper class type into corresponding primitive type, is known as Unboxing. Let’s see the example of unboxing:

  1. class UnboxingExample1{
  2.   public static void main(String args[]){
  3.     Integer i=new Integer(50);
  4.         int a=i;
  5.         System.out.println(a);
  6.  }
  7. }

Test it Now

Output:50

Autoboxing and Unboxing with comparison operators

Autoboxing can be performed with comparison operators. Let’s see the example of boxing with comparison operator:
  1. class UnboxingExample2{
  2.   public static void main(String args[]){
  3.     Integer i=new Integer(50);
  4.         if(i<100){            //unboxing internally
  5.         System.out.println(i);
  6.         }
  7.  }
  8. }

Test it Now

Output:50

Autoboxing and Unboxing with method overloading

In method overloading, boxing and unboxing can be performed. There are some rules for method overloading with boxing:

  • Widening beats boxing
  • Widening beats varargs
  • Boxing beats varargs

1) Example of Autoboxing where widening beats boxing

If there is possibility of widening and boxing, widening beats boxing.
  1. class Boxing1{
  2.   static void m(int i){System.out.println(“int”);}
  3.   static void m(Integer i){System.out.println(“Integer”);}
  4.   public static void main(String args[]){
  5.    short s=30;
  6.    m(s);
  7.  }
  8. }

Test it Now

Output:int

2) Example of Autoboxing where widening beats varargs

If there is possibility of widening and varargs, widening beats var-args.
  1. class Boxing2{
  2.   static void m(int i, int i2){System.out.println(“int int”);}
  3.   static void m(Integer… i){System.out.println(“Integer…”);}
  4.   public static void main(String args[]){
  5.    short s1=30,s2=40;
  6.    m(s1,s2);
  7.  }
  8. }

Test it Now

Output:int int

3) Example of Autoboxing where boxing beats varargs

Let’s see the program where boxing beats variable argument:
  1. class Boxing3{
  2.   static void m(Integer i){System.out.println(“Integer”);}
  3.   static void m(Integer… i){System.out.println(“Integer…”);}
  4.   public static void main(String args[]){
  5.    int a=30;
  6.    m(a);
  7.  }
  8. }

Test it Now

Output:Integer

Method overloading with Widening and Boxing

Widening and Boxing can’t be performed as given below:
  1. class Boxing4{
  2.   static void m(Long l){System.out.println(“Long”);}
  3.   public static void main(String args[]){
  4.    int a=30;
  5.    m(a);
  6.  }
  7. }

Test it Now

Output:Compile Time Error
Next TopicEnum In Java

 

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Autoboxing and Unboxing: The automatic conversion of primitive data types into its equivalent Wrapper type is known as boxing and opposite operation is known as unboxing. This is the new feature of Java5. So java programmer doesn’t need to write the conversion code. Advantage of Autoboxing and Unboxing: No need of conversion between primitives and […]

Tags:

....
...