Sign up ×

A "Heap Pollution" as in Non-Reifiable Types (The Java™ Tutorials > Learning the Java Language > Generics (Updated))

Why is it called that way?

share|improve this question
A good reason for not having any warnings in your project. –  user1249 Jul 8 '12 at 8:00
@ThorbjørnRavnAndersen Good one. But still - there should be a reason / mnemonic device behind that name. It is funny but even google does not seem to know why. –  user18404 Jul 8 '12 at 8:07

1 Answer 1

up vote 32 down vote accepted

Heap pollution simply implies that you have "bad stuff" in your heap. It is an analogy to (for example) water pollution which is where you have "bad stuff" in the water.

Specifically, the bad stuff here is objects of type A where you ought to have objects of type B ... according to the static typing. Some hole in the static typing is allowing the bad stuff to leak into the heap ... where it is liable to cause damage (e.g. unexpected ClassCastExceptions) at runtime.

It is not a particularly good analogy, but it is the terminology that the Java folks coined ... and they have (in effect) defined it in the page that you linked to, and probably other places.


Why should there be? There are lots of words and phrases in use in English where nobody really knows the origin or the reasoning. (In this case, the source is known, and the reasoning are self evident ...)


Not really. It just means that nobody had previously thought to ask for a definition in a place that Google indexes. 1) The usage is restricted to the Java programming AFAIK, and 2) the analogy is obvious ... to most people who would use the term.

share|improve this answer
More precisely, heap pollution can only occur when using generics. If you have code such as: A someVar = ...; The variable someVar can never refer to an object of type B (assuming that A and B are unrelated types). However, if you have List<A> someList = ...; It is possible for someList to contain references to objects of type B, and if this occurs, the heap is said to be polluted. This can occur when code contains unchecked casts (which generate warnings), or mixes raw types and generics, which also generate such warnings. –  Stuart Marks Jul 13 '12 at 16:16
@StuartMarks - this is true, but the OP was looking for the reasons why this is called "heap pollution", not a technical explanation. (Indeed, he already has the technical explanation in the page he linked to!) –  Stephen C Jul 20 '12 at 4:31
@StephenC That's why it's a comment rather than an answer. –  MC Emperor Sep 10 at 14:35
@MCEmperor - And that is why my comment was a comment too. What actually is your point? –  Stephen C Sep 12 at 4:54

Your Answer

