BoxUnbox.java

Download BoxUnbox.java

 1: // Demo of Java 5 auto-boxing and unboxing.  This feature converts primitive
 2: // values to the java.lang Object "wrapped" version, and back, as needed.
 3: //
 4: // Written 8/2005 by Wayne Pollock, Tampa Florida USA
 5: 
 6: class BoxUnbox
 7: {
 8:    public static void main ( String [] args )
 9:    {
10:       Integer num = 5;  // pre-Java5: Integer num = new Integer( 5 );
11:       num += 2;         // pre-Java5: num = new Integer( num.intValue() + 2);
12:       add( 2, num );    // pre-Java5: add( 2, num.intValue() );
13:    }
14: 
15:    static void add ( int i1, int i2 )
16:    {
17:       int sum = i1 + i2;
18:       System.out.println( "Sum of " + i1 + " and " + i2 + " is " + sum );
19:    }
20: }