Wednesday, 17 May 2017

Problems with Generics (type erasure)

Generics are checked at compile-time for type-correctness. The generic type information is then removed in a process called type erasure.

Because of type erasure, type parameters cannot be determined at run-time.
Example, when an ArrayList is examined at runtime, there is no general way to determine whether, before type erasure, it was an ArrayList<Integer> or an ArrayList<Float>.

ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Float> floatList = new ArrayList<Float>();
if (intList.getClass() == floatList.getClass()) { // evaluates to true

A generic class cannot extend the Throwable class in any way, directly or indirectly:
public class GenericException<T> extends Exception

The reason why this is not supported is due to type erasure:
try {
    throw new GenericException<Integer>();
} catch(GenericException<Integer> e) {
} catch(GenericException<String> e) {

Deep dive in Type Erasure
import java.util.HashMap;
import java.util.Map;

public class TypeErasure {
  public static void main(String args[]) {

    Map<String,String> genericMap = new HashMap<>();
    Map nonGenericMap = new HashMap();

    genericMap.put("Rajesh", "Developer");
    String role = genericMap.get("Rajesh");
    System.out.println("Role: "+language);

We instantiate a generic map using the parameterized type as generic String type. It provides us the type safety and helps avoid the need for explicit casting.

Type erasure is a process to remove these types and map it to raw type in bytecode and it is done during compilation by the Java compiler. Java compiler replaces generic type information from the source and adds casts as needed and delivers the bytecode, generics related information are kept as metadata in the bytecode for debugging, reflection purposes. With respect to general runtime execution, the code will look like plain Java without generics.

Let us compile the above given Java source file and then decompile that generated the binary class file.

C:\Users\awadh\Desktop\interview>javap -c TypeErasure
Compiled from ""
public class TypeErasure {
  public TypeErasure();
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":
       4: return

  public static void main(java.lang.String[]);
       0: new           #2                  // class java/util/HashMap
       3: dup
       4: invokespecial #3                  // Method java/util/HashMap."<init>"
       7: astore_1
       8: new           #2                  // class java/util/HashMap
      11: dup
      12: invokespecial #3                  // Method java/util/HashMap."<init>"
      15: astore_2
      16: aload_1
      17: ldc           #4                  // String 1954
      19: ldc           #5                  // String FORTRAN
      21: invokeinterface #6,  3            // InterfaceMethod java/util/Map.put
      26: pop
      27: aload_1
      28: ldc           #4                  // String 1954
      30: invokeinterface #7,  2            // InterfaceMethod java/util/Map.get
      35: checkcast     #8                  // class java/lang/String
      38: astore_3
      39: getstatic     #9                  // Field java/lang/System.out:Ljava/
      42: new           #10                 // class java/lang/StringBuilder
      45: dup
      46: invokespecial #11                 // Method java/lang/StringBuilder."<
      49: ldc           #12                 // String Language:
      51: invokevirtual #13                 // Method java/lang/StringBuilder.ap
      54: aload_3
      55: invokevirtual #13                 // Method java/lang/StringBuilder.ap
      58: invokevirtual #14                 // Method java/lang/
      61: invokevirtual #15                 // Method java/io/PrintStream.printl
      64: return

This is done by the java compiler while compiling java source code to bytecode as part of the process java type erasure. Main reason behind having generics and type erasure is to give the programmer ability to write generic code and type safety. Generics is not forced, the generic type can be directly used as the raw type instead of supplying parameterized type argument. But it is not a good coding practice.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...