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:

    The mother’s and father’s attitudes toward the child correspond to the child’s own needs.... Mother has the function of making him secure in life, father has the function of teaching him, guiding him to cope with those problems with which the particular society the child has been born into confronts him.
    Erich Fromm (1900–1980)

    It is not impossible, of course, after such an administration as Roosevelt’s and after the change in method that I could not but adapt in view of my different way of looking at things, that questions should arise as to whether I should go back on the principles of the Roosevelt administration.... I have a government of limited power under a Constitution, and we have got to work out our problems on the basis of law. Now, if that is reactionary, then I am a reactionary.
    William Howard Taft (1857–1930)

    To play safe, I prefer to accept only one type of power: the power of art over trash, the triumph of magic over the brute.
    Vladimir Nabokov (1899–1977)