Showing posts with label AutoBoxing and UnBoxing. Show all posts
Showing posts with label AutoBoxing and UnBoxing. Show all posts

Tuesday, August 27, 2013

AutoBoxing and UnBoxing


AutoBoxing and UnBoxing

package NewFeatures;

/**
* @author sanjeeva
* boxing - automatic conversion of Primitive Data types into its equivalent Wrapper
* unboxing - opposite operation
* - [ Wrapper ( Primitive : int --> Wrapper : Integer) ]
* comes with Java 5
*
* Advantage of AutoBoxing and unboxing :
* - no need conversion between Primitive and Wrappers manually ( so less coding )
*
*/
public class AutoBoxingUnBoxingExample {

     /**
     * @param args
     */
     public static void main(String[] args) {
        AutoBoxingUnBoxingExample boxingExample = new AutoBoxingUnBoxingExample();
        boxingExample.doExecute();
     }

     private void doExecute(){

          int a = 50;
          Integer b = new Integer(a); //Boxing
          Integer c = 5; //Boxing
          Integer d = b + c;

          System.out.println("a | b | c | d --> " + a + " | " + b + " | " + c + " | " + d);
          
          //----------------------------------
          Integer e = new Integer(100);
          int f = e; //unboxing

          System.out.println("e | f --> " + e + " | " + f);

          if(e < 200) //unboxing
               System.out.println("true");
          }
}