Monday, June 20, 2016

Boxing 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.

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

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. }  

No comments:

Post a Comment