Tuesday, 31 March 2015

Autoboxing and Unboxing in Java

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.        
2.    class BoxingExample1{  
3.      public static void main(String args[]){  
4.        int a=50;  
5.            Integer a2=new Integer(a);//Boxing  
6.      
7.            Integer a3=5;//Boxing  
8.              
9.            System.out.println(a2+" "+a3);  
10.  }   
11. }  
      
Output:50 5

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.        
2.    class UnboxingExample1{  
3.      public static void main(String args[]){  
4.        Integer i=new Integer(50);  
5.            int a=i;  
6.              
7.            System.out.println(a);  
8.     }   
9.    }  
      
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.        
2.    class UnboxingExample2{  
3.      public static void main(String args[]){  
4.        Integer i=new Integer(50);  
5.              
6.            if(i<100){            //unboxing internally  
7.            System.out.println(i);  
8.            }  
9.     }   
10. }  
   

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.        
2.    class Boxing1{  
3.      static void m(int i){System.out.println("int");}  
4.      static void m(Integer i){System.out.println("Integer");}  
5.      
6.      public static void main(String args[]){  
7.       short s=30;  
8.       m(s);  
9.     }   
10. }  

      
Output:int

2) Example of Autoboxing where widening beats varargs

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

Output:int int

3) Example of Autoboxing where boxing beats varargs

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

Output:Integer

Method overloading with Widening and Boxing

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

Output:Compile Time Error


No comments:

Post a Comment

Access attributes in component

NOTE: To access an attribute in a  component , use expressions as  {! v.<Attribute Name>} . ----------------------------------------...