// CSE 341 - Java generics // Example of casting a generic of unknown (wildcard) type to // a known type // // The Java compiler will compile this program (although with a complaint // about unsafe operations). At runtime it will optimistically let this // statement be executed without raising an exception: // LinkedList ilist = (LinkedList) s; // although in fact s isn't necessarily a list of Integers. // However, type safety isn't lost -- the for loop will get an exception // when it finds a non-integer in the list. // Here is the warning the compiler gives: // javac -Xlint:unchecked Wild4.java // Wild4.java:22: warning: [unchecked] unchecked cast // found: java.util.LinkedList // required: java.util.LinkedList // LinkedList ilist = (LinkedList) s; import java.util.LinkedList; import java.util.Iterator; import java.awt.Point; class Wild4 { public static void printAll(LinkedList s) { System.out.println("entering printAll"); LinkedList ilist = (LinkedList) s; System.out.println( "successfully cast LinkedList to LinkedList"); for (Integer i : ilist) { System.out.println("printing a list element"); System.out.println(i); } System.out.println("leavinging printAll"); } public static void main(String[] args) { LinkedList ilist = new LinkedList(); ilist.add(new Integer(3)); ilist.add(new Integer(5)); ilist.add(null); printAll(ilist); LinkedList plist = new LinkedList(); plist.add(new Point(10,20)); printAll(plist); } }