// Demonstrate a raw type. class Gen { T ob; // declare an object of type T // Pass the constructor a reference to // an object of type T. Gen(T o) { ob = o; } // Return ob. T getob() { return ob; } } // Demonstrate raw type. class RawDemo { public static void main(String args[]) { // Create a Gen object for Integers. Gen iOb = new Gen(88); // Create a Gen object for Strings. Gen strOb = new Gen("Generics Test"); // Create a raw-type Gen object and give it // a Double value. Gen raw = new Gen(new Double(98.6)); // Cast here is necessary because type is unknown. double d = (Double) raw.getob(); System.out.println("value: " + d); // The use of a raw type can lead to runtime. // exceptions. Here are some examples. // The following cast causes a runtime error! // int i = (Integer) raw.getob(); // runtime error // This assigment overrides type safety. strOb = raw; // OK, but potentially wrong // String str = strOb.getob(); // runtime error // This assignment also overrides type safety. raw = iOb; // OK, but potentially wrong // d = (Double) raw.getob(); // runtime error } }