Erasure

This lesson explains how erasure works in Java.

We'll cover the following...

Question # 1

What is type erasure?

Consider the following two code snippets:

Raw type

        List list = new LinkedList();
        list.add("Educative");

Generic Type

        List<String> list = new LinkedList<>();
        list.add("Educative");

The bytecode for the above two snippets will be exactly the same! Erasure can be thought of as enforcing type constraints only at compile time and discarding type information at runtime - or erasing type information. If we lose the type information in the second snippet, it'll become equivalent to the raw snippet.

Generics in Java are implemented using erasure which has the following effects on the code:

  • Replaces all type parameters (T, U, V, etc) in generic types (classes, interfaces, etc).

  • Adds implicit casts to preserve type safety. Before generics, the casts were explicitly added by the developer

  • Generates additional methods called bridge methods to preserve polymorphism in extended generic types.

The chief benefit of generics is if your code compiled without any checked warnings, you have a cast-iron guarantee from the platform that the implicit casts added by the compilation of generics never fail. You don't have this guarantee with legacy code that uses explicit casts and it may fail at runtime with a ClassCastException

It is important to realize that the types ArrayList<Integer>, ArrayList<String>, and ArrayList<List<Integer>> are all represented at run-time by the same type, ArrayList! Type erasure ensures that no new classes are created for parameterized types. Convince yourself of this fact by running the code snippet below.

Press + to interact
import java.util.*;
class Demonstration {
public static void main( String args[] ) {
// Raw type
ArrayList al = new ArrayList();
// ArrayList of Strings
ArrayList<String> strs = new ArrayList<>();
// ArrayList of ArrayList of Strings
ArrayList<ArrayList<String>> lstOfStringLsts = new ArrayList<>();
System.out.println("al.getClass().equals(strs.getClass()) = " + al.getClass().equals(strs.getClass()));
System.out.println("al.getClass().equals(lstOfStringLsts.getClass()) = " + al.getClass().equals(lstOfStringLsts.getClass()));
}
}

Question # 2

Can you give examples of type erasure?

During the type ...