Friday, February 20, 2009

What is type erasure in Java?

I had read about type erasure in Java a long time back. However, today when a friend asked me a question related to type erasure, I found myself not quite certain of the answer.

I read up on it again and here is what I learned.

Angelika Langer has an excellent FAQ, where she explains generics, and type erasure. According to the FAQ:

A process that maps a parameterized type (or method) to its unique byte code representation by eliding type parameters and arguments.

OK, let's understand what that means. Below is a simple class which uses the generified version of Java Lists.









package net.adaptivelearningonline.examples.generics;

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);
    }
  }
}




I am going to take the bytecode generated by the above class and decompile it using JAD.

Here is the decompiled Java class:









// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   GenericsErasure.java

package net.adaptivelearningonline.examples.generics;

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();

    }
}





As you can see, the decompiled code has absolutely no trace of any generics information. This is because of erasure. When the Java compiler compiles client code which uses a library that has been generified, the client code is compiled in such a way that all generics information is erased from the bytecode.

This also explains the fact that generics information in client code is meant purely for the compiler to do type checking. Once the compiler has done it's job, this information is discarded.

The reason why Java uses type erasure is to maintain backwards compatibility. Let's assume you wrote some code a few years back which used Java Lists, before generics was introduced in the language. Obviously your code had no mention of generics. Now, when engineers at Sun decided to introduce generics, they did not want to break code which people had already written. One possible way was to ensure that client code which uses generics in the classes they invoke, never carries any information about generics in the compiled code. So the above class when compiled should not carry any information about generics. Because compiled client code never carries information about generics anyways, library implementers are free to add generics to their code without the worry of breaking anyone's old code.

Bruce Eckel has written a rather lenghty but good article on generics and erasure.

4 comments:

jeet said...

Thanks for this article.It is helpful.

Anonymous said...

Awesome article.... keep it up mate

Dhiral V Pandya said...

great example... keep it up ...

Anonymous said...

> Bruce Eckel has written a rather lenghty but good article on generics and erasure.

Eckel's article can be found here: https://web.archive.org/web/20071224095307/http://www.mindview.net/WebLog/log-0060