// Demonstrate a raw type.
class Gen<T> {
T ob;
Gen(T o) {
ob = o;
}
T getob() {
return ob;
}
}
public class MainClass {
public static void main(String args[]) {
Gen<Integer> iOb = new Gen<Integer>(88);
Gen<String> strOb = new Gen<String>("Generics Test");
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);
strOb = raw; // OK, but potentially wrong
String str = strOb.getob();
// This assignment also overrides type safety.
raw = iOb; // OK, but potentially wrong
d = (Double) raw.getob();
}
}
|