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.


It's basically the way that generics are implemented in Java via compiler trickery. The compiled generic code actually just uses java.lang.Object wherever you talk about T (or some other type parameter) - and there's some metadata to tell the compiler that it really is a generic type.

When you compile some code against a generic type or method, the compiler works out what you really mean (i.e. what the type argument for T is) and verifies at compile time that you're doing the right thing, but the emitted code again just talks in terms of java.lang.Object - the compiler generates extra casts where necessary. At execution time, a List<String> and a List<Date> are exactly the same; the extra type information has been erased by the compiler.

Compare this with, say, C#, where the information is retained at execution time, allowing code to contain expressions such as typeof(T) which is the equivalent to T.class - except that the latter is invalid. (There are further differences between .NET generics and Java generics, mind you.) Type erasure is the source of many of the "odd" warning/error messages when dealing with Java generics.

Other resources:

  • Oracle documentation
  • Wikipedia
  • Gilad Bracha's Java generics guide (PDF - highly recommended; link may need to change periodically)
  • Angelika Langer's Java Generics FAQ

Tags:

Java

Generics