What is the concept of erasure in generics in Java?
Just as a side-note, it is an interesting exercise to actually see what the compiler is doing when it performs erasure -- makes the whole concept a little easier to grasp. There is a special flag you can pass the compiler to output java files that have had the generics erased and casts inserted. An example:
javac -XD-printflat -d output_dir SomeFile.java
The -printflat
is the flag that gets handed off to the compiler that generates the files. (The -XD
part is what tells javac
to hand it to the executable jar that actually does the compiling rather than just javac
, but I digress...) The -d output_dir
is necessary because the compiler needs some place to put the new .java files.
This, of course, does more than just erasure; all of the automatic stuff the compiler does gets done here. For example, default constructors are also inserted, the new foreach-style for
loops are expanded to regular for
loops, etc. It is nice to see the little things that are happening automagically.
Erasure, literally means that the type information which is present in the source code is erased from the compiled bytecode. Let us understand this with some code.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class GenericsErasure {
public static void main(String args[]) {
List<String> list = new ArrayList<String>();
list.add("Hello");
Iterator<String> iter = list.iterator();
while(iter.hasNext()) {
String s = iter.next();
System.out.println(s);
}
}
}
If you compile this code and then decompile it with a Java decompiler, you will get something like this. Notice that the decompiled code contains no trace of the type information present in the original source code.
import java.io.PrintStream;
import java.util.*;
public class GenericsErasure
{
public GenericsErasure()
{
}
public static void main(String args[])
{
List list = new ArrayList();
list.add("Hello");
String s;
for(Iterator iter = list.iterator(); iter.hasNext(); System.out.println(s))
s = (String)iter.next();
}
}