Sunday, March 4, 2012

Boxing and Unboxing in Java

                                                  Boxing and Unboxing in Java



  • Boxing and Unboxing make using wrapper classes more convenient.
  • In Java 5, new feature came, and it was Auto-boxing and Auto-unboxing.
  • In the old, pre-Java 5 days, if you wanted to make a wrapper, unwrap it, use it, and then rewrap it, you might do something like this:



                Integer y = new Integer(56);       // make it
                int x= y.intValue();                      // unwrap it
                x++;                                              // use it
                y = new Integer(x);                     // re-wrap it
                System.out.println("y =" + y);    // print it

  • Now improved version of Java 5 you can say, 
                Integer y = new Integer(56);             //make it
                y++;                        //unwrap-it, increment-it, rewrap-it
                System.out.println("y = " + y);    // print it



  • Both Examples produce the output::



                                y=57

  • Behind the scenes, the compiler does the unboxing and reassignment for  you.
  •  We knew that wrapper objects are immutable, this example appears to contradict that statement. It sure looks like y's value changes fro 56 to 57.
  • What actually happened, is that second wrapper object was created and its value was set to 57.
  • We could only prove it as:-
          Integer y = 56;                       // make a wrapper
          Integer x = y;            // assign a second ref var to the wrapper


           System.out.println(y==x);
           y++;               //unwrap, use , rewrap
          System.out.println(x+ " " + y);       // print values


         System.out.println(y==x);              //verify that they refer
                                                                 // to different objects



  •   Which produces the output:
                    true
                    56 57
                    false

  • So under the covers when the compiler  got to the line y++; it had to substitute something like this:
                   int x2 = y.intValue();                 // unwrap it
                   x2++;
                   y = new Integer(x2);                 // rewrap-it


                               
                              Where Boxing Can Be Used



  • It is very common to use wrappers in conjunction with collections. 
  • Any time you want your collection to hold objects and primitives, you 'll want to us wrappers to make those primitives collection-compatible.
  • The general rule is that boxing and unboxing work wherever you can normally use a primitive or a wrapped object.
      
              class UseBoxing {
                   public static void main(String args[])
                   {
                     UseBoxing u = new UseBoxing();
                        u.go(5);
                     }
                      boolean go(Integer i)
                        {
                        Boolean ifSo = true;      //boxes the int 
                        Short s = 400;                  //boxes the literal
                        if(ifSo)
                        {                                        // unboxing
                         System.out.println(++s);   //unboxes, increments, 
                                                                   // reboxes.
                          }
                          return !ifSo;
                          }
                         }