:Heap pollution

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.

{{cite web|title=The Java SE Tutorials|url=https://docs.oracle.com/javase/tutorial/java/generics/nonReifiableVarargsType.html#heap_pollution|publisher=Oracle|accessdate=16 July 2014}}

This situation is normally detected during compilation and indicated with an unchecked warning. Later, during runtime heap pollution will often cause a ClassCastException.

{{cite web|last1=Langer|first1=Angelika|title=Java Generics FAQs: Heap pollution|url=https://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#Topic2|website=angelikalanger.com/|accessdate=15 July 2014}}

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.

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 context

public class HeapPollutionDemo

{

public static void main(String[] args)

{

Set s = new TreeSet();

Set ss = s; // unchecked warning

s.add(new Integer(42)); // another unchecked warning

Iterator iter = ss.iterator();

while (iter.hasNext())

{

String str = iter.next(); // ClassCastException thrown

System.out.println(str);

}

}

}

Further reading

  • {{cite book | title=The Java Language Specification, Java SE 8 Edition | chapter=4.12.2 | last1=Gosling | first1=James | last2=Joy | first2=Bill | last3=Steele | first3=Guy | last4=Bracha | first4=Gilad | last5=Buckley | first5=Alex | publisher=Addison-Wesley | year=2014 | isbn=978-0-13-390069-9 | pages=81–82 | chapter-url=https://books.google.com/books?id=mh-KAwAAQBAJ&pg=PA81}}
  • {{cite book | title=Beginning Java 7 | last=Friesen | first=Jeff | page=211 | url=https://books.google.com/books?id=CwSaQpCtfPkC&pg=PA211 | series=Expert's voice in Java | isbn=978-1430239093 | date=2011 | publisher=Apress}}
  • {{cite book | title=Java 7 New Features Cookbook | pages=38–40 | url=http://dl.e-book-free.com/2013/07/java_7_new_features_cookbook.pdf | publisher=Packt Publishing | date=2012 | last1=Reese | first1=Richard | last2=Reese | first2=Jennifer | isbn=978-1-84968-562-7}}
  • {{cite book | doi=10.1007/978-3-540-79980-1 | last1=Stenzel | first1=Kurt | last2=Grandy | first2=Holger | last3=Reif | first3=Wolfgang | chapter=Verification of Java Programs with Generics | series=Lecture Notes in Computer Science | pages=315–329 | year=2008 | title=Algebraic Methodology and Software Technology | volume=5140 | isbn=978-3-540-79979-5 }}{{subscription required}}

References

{{reflist}}

Category:Java platform

{{prog-lang-stub}}