Heap pollution: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Cewbot (talk | contribs)
m Normalize {{Multiple issues}}: Remove {{Multiple issues}} for only 1 maintenance template(s): Underlinked
Line 1: Line 1:
{{Multiple issues|
{{Underlinked|date=September 2015}}
{{Underlinked|date=September 2015}}
{{Orphan|date=July 2014}}
{{Orphan|date=July 2014}}
}}


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.<ref name=Java>{{cite web|title=The Java SE Tutorials|url=http://docs.oracle.com/javase/tutorial/java/generics/nonReifiableVarargsType.html#heap_pollution|publisher=Oracle|accessdate=16 July 2014}}</ref> This situation is normally detected during compilation and indicated with an unchecked warning.<ref name=Java/> Later, during runtime heap pollution will often cause a ClassCastException.<ref name=Langer>{{cite web|last1=Langer|first1=Angelika|title=Java Generics FAQs: Heap pollution|url=http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#Topic2|website=angelikalanger.com/|accessdate=15 July 2014}}</ref>
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.<ref name=Java>{{cite web|title=The Java SE Tutorials|url=http://docs.oracle.com/javase/tutorial/java/generics/nonReifiableVarargsType.html#heap_pollution|publisher=Oracle|accessdate=16 July 2014}}</ref> This situation is normally detected during compilation and indicated with an unchecked warning.<ref name=Java/> Later, during runtime heap pollution will often cause a ClassCastException.<ref name=Langer>{{cite web|last1=Langer|first1=Angelika|title=Java Generics FAQs: Heap pollution|url=http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#Topic2|website=angelikalanger.com/|accessdate=15 July 2014}}</ref>

Revision as of 03:16, 31 May 2020

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]

A source of heap pollution in Java arises from the fact that 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. Indeed, 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, it is possible that a variable of a parameterized type refers 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<Integer>();
      Set<String> ss = s;            // unchecked warning
      s.add(new Integer(42));        // another unchecked warning
      Iterator<String> iter = ss.iterator();
      while (iter.hasNext())
      {
         String str = iter.next();   // ClassCastException thrown
         System.out.println(str);
      }
   }
}

Further reading

  • Gosling, James; Joy, Bill; Steele, Guy; Bracha, Gilad; Buckley, Alex (2014). "4.12.2". The Java Language Specification, Java SE 8 Edition. Addison-Wesley. pp. 81–82. ISBN 978-0-13-390069-9.
  • Friesen, Jeff (2011). Beginning Java 7. Expert's voice in Java. Apress. p. 211. ISBN 1430239093.
  • Reese, Richard; Reese, Jennifer (2012). Java 7 New Features Cookbook (PDF). Packt Publishing. pp. 38–40. ISBN 978-1-84968-562-7.
  • Stenzel, Kurt; Grandy, Holger; Reif, Wolfgang (2008). "Verification of Java Programs with Generics". Algebraic Methodology and Software Technology. Lecture Notes in Computer Science. Vol. 5140. pp. 315–329. doi:10.1007/978-3-540-79980-1. ISBN 978-3-540-79979-5.(subscription required)

References

  1. ^ a b "The Java SE Tutorials". Oracle. Retrieved 16 July 2014.
  2. ^ a b Langer, Angelika. "Java Generics FAQs: Heap pollution". angelikalanger.com/. Retrieved 15 July 2014.