...
Mixing generically typed code with raw typed code is one common source of heap pollution. Generic types were unavailable prior to Java 5, so popular interfaces such as the Java Collection Framework relied on raw types. Mixing generically typed code with raw typed code allowed developers to preserve compatibility between nongeneric legacy code and newer generic code but also gave rise to heap pollution. Heap pollution can occur if the program performs some operation involving a raw type that would give rise to a compile-time unchecked warning.
Code Block |
---|
List list = new ArrayList();
List<String> ls = list; // Produces unchecked warning
|
When generic and nongeneric types are used together correctly, these warnings can be ignored; at other times, these warnings can denote potentially unsafe operations. Mixing generic and raw types is allowed provided that heap pollution does not occur. For For example, consider the following code snippet.
Code Block |
---|
List list = new ArrayList();
List<String> ls = list; // Produces unchecked warning
|
In some cases, it is possible that a compile-time unchecked warning will not be generated. According to the JLS, §4.12.2, "Variables of Reference Type" [JLS 2015]:
...