Heap pollution explained
In the Java programming language, heap pollution is a situation that arises when a variable of a parameterized type refers to an object that is not of that parameterized type.[1] This situation is normally detected during compilation and indicated with an unchecked warning.[1] Later, during runtime heap pollution will often cause a ClassCastException.[2]
Heap pollution in Java can occur when type arguments and variables are not reified at run-time. As a result, different parameterized types are implemented by the same class or interface at run time. All invocations of a given generic type declaration share a single run-time implementation. This results in the possibility of heap pollution.[2]
Under certain conditions, a variable of a parameterized type may refer to an object that is not of that parameterized type. The variable will always refer to an object that is an instance of a class that implements the parameterized type.
Heap Pollution in a non-varargs contextpublic class HeapPollutionDemo
Further reading
- Book: The Java Language Specification, Java SE 8 Edition . 4.12.2 . Gosling . James . Joy . Bill . Steele . Guy . Bracha . Gilad . Buckley . Alex . Addison-Wesley . 2014 . 978-0-13-390069-9 . 81 - 82 . https://books.google.com/books?id=mh-KAwAAQBAJ&pg=PA81.
- Book: Friesen, Jeff . Beginning Java 7 . 211 . Expert's voice in Java . 978-1430239093 . 2011 . Apress.
- Book: Java 7 New Features Cookbook . 38 - 40 . Packt Publishing . 2012 . Reese . Richard . Reese . Jennifer . 978-1-84968-562-7.
- Book: 10.1007/978-3-540-79980-1 . Stenzel . Kurt . Grandy . Holger . Reif . Wolfgang . Verification of Java Programs with Generics . Lecture Notes in Computer Science . 315 - 329 . 2008 . Algebraic Methodology and Software Technology . 5140 . 978-3-540-79979-5 .
Notes and References
- Web site: The Java SE Tutorials. Oracle. 16 July 2014.
- Web site: Langer. Angelika. Java Generics FAQs: Heap pollution. angelikalanger.com/. 15 July 2014.