/* * TGn03: Before Autoboxing/Unboxing. Collection objects, such as * ArrayList, can only hold objects. The base types (int, char, double, * etc.) are not part of the Java object hierarchy. In order to store * them into an ArrayList, we need to 'hide' them within objects. Java * provides a set of "wrapper" classes for this purpose. For example, * the class Double is the wrapper class for double values. * This example shows what needs to be done to store doubles into an * ArrayList in versions of Java prior to 1.5. * * If you compile this example with a 1.5 (5.0) or later version of Java, * you will get a warning from the compiler that "unchecked or unsafe * operations" are being used. This illustrates the need for generics: * Without them, the compiler was unable to help the programmer recognize * code that was potentially mixing object references in useless ways. * Now Java can recognize such situations and provide feedback. */ import java.io.*; import java.util.*; public class TGn03 { public static void main (String [] args) { ArrayList myList; Double dblObj1, dblObj2; double temp1, temp2; myList = new ArrayList(); temp1 = 72.8; dblObj1 = new Double(temp1); // wrap the double value myList.add(dblObj1); // wrapped double can be added dblObj2 = (Double) myList.get(0); // retrieve object reference & cast temp2 = dblObj2.doubleValue(); // extract value from wrapper System.out.println(temp2); } }