Simplifying the use of wrapper class objects

  • The official syntax to create a wrapper class Integer object is:

      Integer i1 = new Integer(99); 

  • In contrast, creating an int primitive typed variable is much simpler:

     int i1 = 99;
    


  • To simplify the use of wrapper class objects, the Java provides a simplified syntax:

      Integer i1 = 99 ;  // Same as:   Integer i1 = new Integer(99)

    This goes for every wrapper class !!

Autoboxing

  • The Java compiler has the following special mechanism/rule built in:

      When assigning a primitive value x to  a wrapper class object:
    
            WrapperClass var  =   x 
    
      the Java compiler will replace  x  with:
    
            WrapperClass var  =  new WrapperClass( x ) 

    Example:

            Integer i2 =  99 ;
    
      ===>  Integer i2 = new Integer( 99 ) 


  • This automatic replacement mechanism is called autoboxing

  • Boxing = converting a primitive value to its wrapper object

Autoboxing   example

  • Example using the simplified syntax to create wrapper objects:

        public static void main(String[] args)
        {
            Integer i1 = new Integer(99);  // Official syntax
            System.out.println( i1 );
            
            Integer i2 =  99 ;             //  new Integer(99)
            System.out.println( i2 );
    
            Double d1 = new Double(88.0);  // Official syntax
            System.out.println( d1 );
            
            Double d2 =  88.0 ;            //  new Double(88.0)
            System.out.println( d2 );
        } 


  • Autoboxing is a built-in service provided by the Java compiler (to make using wrapper object more convenient)

DEMO: demo/12-wrapper/02-simplified/AutoBoxing.java

Another simplifying rule for wrapper class objects

  • The official syntax to use the value stored in a wrapper class Integer object is:

      Integer o = new Integer(99);  // Integer object
      int x = 1;                    // Primitive integer
    
      x = x + o.intValue(); // Must use accessor method (private)
    

    Writing the official syntax is very tedious


  • To simplify the use of wrapper class objects, the Java provides a simplified syntax:

      x = x + o ; // Same as:   x = x + o.intValue()

    This goes for every wrapper class !!

Auto-unboxing

  • The Java compiler has another special mechanism/rule built in:

      When using a wrapper object  o in an expression (= computation)
      the Java compiler will replace:
     
             o  ===>   o.accessorMethod()  

    Example:

      Integer o = new Integer(99);  // Integer object
      int x = 1;                    // Primitive integer
    
      x = x +  o ;     ===>     x = x +  o.intValue() ; 

  • This automatic replacement mechanism is called auto-unboxing

  • Unboxing = converting a wrapper object to a primitive value

Auto-unboxing   example

  • Example using the simplified syntax to use the value stored in wrapper objects:

     public static void main(String[] args)
     {
        Integer i1 = new Integer(99); 
            
        int x;
     
        x = 4 + i1.intValue(); // Official syntax
        x = 5 + i1;            // Auto-Unboxing
     }


  • Auto-unboxing is a built-in service provided by the Java compiler (to make using wrapper object more convenient)

DEMO: demo/12-wrapper/02-simplified/AutoUnboxing.java