Friday, May 18, 2012

Type Erasure in Java

While reading some posts online I came across the following post -
List<integer> list = new ArrayList<integer>();
List&lt;integer&gt; list = new ArrayList&lt;integer&gt;();
List list1 = new ArrayList();
list1.add(&quot;Some String&quot;);

This code compiles and executes as well, this works because of Type Erasure concept in Java. I knew about Type Erasure but while reading more about it on internet I found some posts which were questioning on Type Erasure concept.
Following code makes more confusing about Type Erasure concept, if compiler removes all the information about type parameters and type arguments then how does following code works??

class A<T>{
class B extends A<String>{
 public B() {

public static void main(String args[]){
     new B();

Output of above code-
Well some people might get confused by this but if you read carefully, Type Erasure docs says -
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.
Compiler does removes all the type parameter and type arguments information within the class but it maintains the information about the super class so the type arguments with it, and that is the reason why above code prints the java.lang.String.
Now lets see the first code snippet again-
List<Integer>list = new ArrayList<Integer>();
List list1 = new ArrayList();
list1.add("Some String");
This will not work with above code

String str = list.get(2);

Compiler will give error for above statement due to generics, but following code will work

Object str = list.get(2);

While iterating through list you can check data from the list by
for( int i = 0 ; i < list.size() ; i++ ){
     Object data = list.get(i);
     if(data instanceof Integer){
     if(data instanceof String){
Type Erasure concept allows Java code to work with the old Java libraries which were written before generics.

No comments:

Post a Comment

Ads Inside Post