Generics in Java - Problems With Type Erasure

Problems With Type Erasure

Generics are checked at compile-time for type-correctness. The generic type information is then removed in a process called type erasure. For example, List will be converted to the non-generic type List, which ordinarily contains arbitrary objects. The compile-time check guarantees that the resulting code is type-correct.

As a result of type erasure, type parameters cannot be determined at run-time. For example, when an ArrayList is examined at runtime, there is no general way to determine whether, before type erasure, it was an ArrayList or an ArrayList. Many people are dissatisfied with this restriction. There are partial approaches. For example, individual elements may be examined to determine the type they belong to; for example, if an ArrayList contains an Integer, that ArrayList was presumably parameterized with Integer. Reflection can also determine the type parameter. However, no approach works in all cases.

Demonstrating this point, the following code outputs "Equal":

ArrayList li = new ArrayList; ArrayList lf = new ArrayList; if (li.getClass == lf.getClass) // evaluates to true System.out.println("Equal");

Java generics differ from C++ templates. Java generics generate only one compiled version of a generic class or function regardless of the number of parameterizing types used. Furthermore, the Java run-time environment does not need to know which parameterized type is used because the type information is validated at compile-time and is not included in the compiled code. Consequently, instantiating a Java class of a parameterized type is impossible because instantiation requires a call to a constructor, which is unavailable if the type is unknown.

For example, the following code will not compile:

T instantiateElementType(List arg) { return new T; //causes a compile error }

Because there is only one copy per generic class at runtime, static variables are shared among all the instances of the class, regardless of their type parameter. As a result, the type parameter cannot be used in the declaration of static variables or in static methods.

Read more about this topic:  Generics In Java

Famous quotes containing the words problems with, problems and/or type:

    In many ways, life becomes simpler [for young adults]. . . . We are expected to solve only a finite number of problems within a limited range of possible solutions. . . . It’s a mental vacation compared with figuring out who we are, what we believe, what we’re going to do with our talents, how we’re going to solve the social problems of the globe . . .and what the perfect way to raise our children will be.
    Roger Gould (20th century)

    I have said many times, and it is literally true, that there is absolutely nothing that could keep me in business, if my job were simply business to me. The human problems which I deal with every day—concerning employees as well as customers—are the problems that fascinate me, that seem important to me.
    Hortense Odlum (1892–?)

    He turned out to belong to the type of publisher who dreams of becoming a male muse to his author, and our brief conjunction ended abruptly upon his suggesting I replace chess by music and make Luzhin a demented violinist.
    Vladimir Nabokov (1899–1977)