Java JVM – Improving Java to Eliminate Type Erasure

javajvm

The official Java tutorial on generics explains type erasure and why it was added to the compiler:

When a generic type is instantiated, the compiler translates those types by a technique called type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method. Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics.

This most likely was a pragmatic approach, or perhaps the least painful one. However, now that generics is widely supported across the industry, what can be done in order for us to not need type erasure? Is it feasible with out needing to break backwards compatibility, or if it is feasible, is it practical?

Has the last statement in the quote above become self referential? That is: "type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created with Java versions that perform type erasure."

Best Answer

The end-of-life applies to the Java Development Toolkit and Java Runtime Environment. And only the Oracle (Sun) versions. But it does not apply to applications written by third parties. The intention is to never break code that has ever run on the JVM, thus it's unlikely Java will ever stop doing type erasure.

Of course C# also introduced generics in later version in backward-compatible manner without doing type erasure, but it basically meant duplicating all the collection classes. Which I suppose is what Java designers don't want to do and thus why they chose type erasure in the first place. Without value types the advantage of non-type-erased generics is not that big.

Related Topic