Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot

...

If a parameterized type tries to access an object that is not of the parameterized type, heap pollution results. For instance, consider the code snippet below.

Code Block
List l = new ArrayList&lt;Integer&gt;ArrayList<Integer>();
List&lt;String&gt;List<String> ls = l; // Produces unchecked warning

...

Code Block
bgColor#FFCCCC
public class MixedTypes {
  @SuppressWarnings(&quot;unchecked&quot;"unchecked")
  private static void addToList(List list, Object obj) {
    list.add(obj); // Unchecked warning
  }
  private static void print() {
    List&lt;String&gt;List<String> list = new ArrayList&lt;String&gt;ArrayList<String> ();
    addToList(list, 1);
    System.out.println(list.get(0));
  }
  public static void main(String[] args) {
    MixedTypes.print();
  }
}

...

Code Block
Exception in thread &quot;main&quot;"main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
	at Raw.print(Test.java:11)
	at Raw.main(Test.java:14)

...

Code Block
bgColor#ccccff
class Parameterized {
  private static void addToList(List&lt;String&gt;List<String> list, String str) {
    list.add(str);     // Unchecked warning
  }
  private static void print() {
    List&lt;String&gt;List<String> list = new ArrayList&lt;String&gt;ArrayList<String> ();
    addToList(list, &quot;1&quot;"1");
    System.out.println(list.get(0));
  }
  public static void main(String[] args) {
    Parameterized.print();
  }
}

...

Code Block
bgColor#FFCCCC
class BadListAdder {
  @SuppressWarnings(&quot;unchecked&quot;"unchecked")
  private static void addToList(List list, Object obj) {
    list.add(obj);     // Unchecked warning
  }
  private static &lt;T&gt;<T> void printOne(T type) {
    if (!(type instanceof Integer || type instanceof Double)) {
      System.out.println(&quot;"Cannot print in the supplied type&quot;");
    }
    List&lt;T&gt;List<T> list = new ArrayList&lt;T&gt;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); 
    BadListAdder.printOne(d);
    System.out.println(i);
    BadListAdder.printOne(i);
  }
}

...

Code Block
bgColor#ccccff
class GoodListAdder {
  private static void addToList(List&lt;Integer&gt;List<Integer> list, Integer i) {
    list.add(i);
  }
  private static void addToList(List&lt;Double&gt;List<Double> list, Double d) {
    list.add(d);
  }
  private static &lt;T&gt;<T> void printOne(T type) {
    if (type instanceof Integer) {
      List&lt;Integer&gt;List<Integer> list = new ArrayList&lt;Integer&gt;ArrayList<Integer>();
      addToList(list, 1);
      System.out.println(list.get(0));
    }
    else if (type instanceof Double) {
      List&lt;Double&gt;List<Double> list = new ArrayList&lt;Double&gt;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(&quot;"Cannot print in the supplied type&quot;");
    }
  }
  public static void main(String[] args) {
    double d = 1;
    int i = 1;
    System.out.println(d);
    GoodListAdder.printOne(d);
    System.out.println(i);
    GoodListAdder.printOne(i);
  }
}

...

Code Block
if(o instanceof Set) { // Raw type
Set&lt;?&gt;Set<?> m = (Set&lt;?&gt;Set<?>) o; // Wildcard type 
...
}

...

Wiki Markup
\[[Langer 08|AA. Java References#Langer 08]\] Topic 3, &quot;"[Coping with Legacy|http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#Topic3]&quot;"
[[Bloch 08|AA. Java References#Bloch 08]\] Item 23: &quot;"Don't use raw types in new code&quot;"
[[Bloch 07|AA. Java References#Bloch 07]\] Generics, 1. &quot;"Avoid Raw Types in New Code&quot;"
\[[Naftalin 06b|AA. Java References#Naftalin 06b]\] &quot;"Principle of Indecent Exposure&quot;"
[[JLS 05|AA. Java References#JLS 05]\] 4.8 &quot;"Raw types&quot;" and 5.1.9 &quot;"Unchecked Conversion&quot;"

...

MSC04-J. Carefully design interfaces before releasing them&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      49. Miscellaneous (MSC)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IDS15-J. Library methods should validate their parameters      MSC06-J. Finish every set of statements associated with a case label with a break statement