What is Type Erasure?

Since Java 5, Java has come up with the concept of Generics. Idea of generics came from the fact that developers at times faced issue with assignment of data types to Lists and other structure. In case a wrong type was assigned or was tried to fetched, error could be caught at run time only


ArrayList arr=new ArrayList();
Integer i=arr.getIndex(0); //run time error

So Java gave us an option

ArrayList arr=new ArrayList();
Integer i=arr.getIndex(0); //compile time error

But we should note that Generics is a compile time concept. When Java convert the code to bytecode for JVM, it removes all generics related info. So finally

ArrayList arr=new ArrayList();

will be converted to

ArrayList arr=new ArrayList();

This concept is known as Type Erasure. This was necessary for Java in order to give backward compatibility, otherwise the code written earlier would not have been supported by newer JVMs.