/* * T01n16.java -- A demonstration of wrapper classes and autoboxing and * unboxing. See comments below. */ //import java.util.*; public class T01n16 { public static void main (String [] args) { Integer intObject1, intObject2; // Wrapper class objects for the demo int content1, content2; // To hold wrapper object content /* The following is how you had to place primitive types * in wrapper objects (and extract them) before Java 1.5. * It looks like you'd probably expect it to look. * Trouble is, it's a pain -- lots of clutter to do * something ordinary. */ intObject1 = new Integer (17); // Create our own "box" (wrapper) content1 = intObject1.intValue(); // Explicitly access box conent System.out.println("The value held in intObject1 is " + content1); /* In Java 1.5 and later, we can mix previously * incompatible items to satisfy our goal of reducing * clutter. Java still does the same stuff as we used to * have to do, it just automates it for us. This may not * seem like a big deal, but it can really clean up code * that would otherwise be a huge mess. */ intObject2 = 22; // This demonstrates autoboxing. content2 = intObject2; // And this is unboxing. System.out.println("The value held in intObject2 is " + content2); } }