...
Code Block | ||
---|---|---|
| ||
class BadListAdderListAdder { @SuppressWarnings("unchecked") private static void addToList(List list, Object obj) { list.add(obj); // Unchecked warning } private static <T> void printOne(T type) { if (!(type instanceof Integer || type instanceof Double)) { System.out.println("Cannot print in the supplied type"); } List<T> list = new ArrayList<T>(); addToList(list, 1); System.out.println(list.get(0)); } public static void main(String[] args) { double d = 1; int i = 1; System.out.println(d); BadListAdderListAdder.printOne(d); System.out.println(i); BadListAdderListAdder.printOne(i); } } |
However, despite list
being correctly parameterized, this method prints '1' and never '1.0' because the int
value '1' is always added to list
without being type checked. This code produces the following output:
...
Code Block | ||
---|---|---|
| ||
class GoodListAdderListAdder { private static <T> void addToList(List<T> list, T t) { list.add(t); // No warning generated } private static <T> void printOne(T type) { if (type instanceof Integer) { List<Integer> list = new ArrayList<Integer>(); addToList(list, 1); System.out.println(list.get(0)); } else if (type instanceof Double) { List<Double> list = new ArrayList<Double>(); // This will not compile if addToList(list, 1) is used addToList(list, 1.0); System.out.println(list.get(0)); } else { System.out.println("Cannot print in the supplied type"); } } public static void main(String[] args) { double d = 1; int i = 1; System.out.println(d); GoodListAdderListAdder.printOne(d); System.out.println(i); GoodListAdderListAdder.printOne(i); } } |
This code compiles cleanly and produces the correct output:
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="fa6461553f58d872-ba5ad7c7-44ad49d0-ba6a8966-56a59948b6f23125fe45e681"><ac:plain-text-body><![CDATA[ | [[Bloch 2008 | AA. Bibliography#Bloch 08]] | Item 23: "Don't use raw types in new code" | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="f76107c5b7847327-a2a5c0e4-4b954840-a51ba11e-8cfe6b0b49a149a561eaa538"><ac:plain-text-body><![CDATA[ | [[Bloch 2007 | AA. Bibliography#Bloch 07]] | Generics, 1. "Avoid Raw Types in New Code" | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3f827cbc35c4cdb8-2f6ecc69-427e47aa-89ef99e5-770e4d6bd74b40d8815b294a"><ac:plain-text-body><![CDATA[ | [[Bloch 2005 | AA. Bibliography#Bloch 05]] | Puzzle 88: Raw Deal | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="36361e2c3d05224e-f1736bb1-41a74429-950b930a-062316886db6e0f4e35343f7"><ac:plain-text-body><![CDATA[ | [[Darwin 2004 | AA. Bibliography#Darwin 04]] | 8.3 Avoid Casting by Using Generics | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="10ee5884c02abbba-65256c2e-4b374951-90ff9472-df3e3fc5c6b898c83496ea26"><ac:plain-text-body><![CDATA[ | [[JavaGenerics 2004 | AA. Bibliography#JavaGenerics 04]] |
| ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="bbfd7d1ad99f670c-d685c8ba-42744d1b-9bae8e3b-27a1ed289f97d6189a04095d"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. Bibliography#JLS 05]] | [Chapter 5 "Conversions and Promotions" | http://java.sun.com/docs/books/jls/third_edition/html/conversions.html] | ]]></ac:plain-text-body></ac:structured-macro> |
| |||||
| |||||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="69bef24786d39258-a8ef8752-44a34288-87978366-d51720962b1e7889c4ed96c3"><ac:plain-text-body><![CDATA[ | [[Langer 2008 | AA. Bibliography#Langer 08]] | Topic 3, "[Coping with Legacy | http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Topic3]" | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="29a5ca9782da9de3-9e561bfc-4aad4ca0-a1568e8d-a919cbdedb557d6c7d17ea1f"><ac:plain-text-body><![CDATA[ | [[Naftalin 2006 | AA. Bibliography#Naftalin 06]] | Chapter 8, "Effective Generics" | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="c25581f528a3adae-ab857391-40854fe1-a1e5b6ec-e293230eee8a7d3488de1090"><ac:plain-text-body><![CDATA[ | [[Naftalin 2006b | AA. Bibliography#Naftalin 06b]] | "Principle of Indecent Exposure" | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="0965d3f3ba2996b5-d06f71ba-41014c95-a70fa11e-062c2a8ad0db9a6d28d4460a"><ac:plain-text-body><![CDATA[ | [[Schildt 2007 | AA. Bibliography#Schildt 07]] | "Create a checked collection" | ]]></ac:plain-text-body></ac:structured-macro> |
...