Java generics type erasure: when and what happens?

102,758

Solution 1

Type erasure applies to the use of generics. There's definitely metadata in the class file to say whether or not a method/type is generic, and what the constraints are etc. But when generics are used, they're converted into compile-time checks and execution-time casts. So this code:

List<String> list = new ArrayList<String>();
list.add("Hi");
String x = list.get(0);

is compiled into

List list = new ArrayList();
list.add("Hi");
String x = (String) list.get(0);

At execution time there's no way of finding out that T=String for the list object - that information is gone.

... but the List<T> interface itself still advertises itself as being generic.

EDIT: Just to clarify, the compiler does retain the information about the variable being a List<String> - but you still can't find out that T=String for the list object itself.

Solution 2

The compiler is responsible for understanding Generics at compile time. The compiler is also responsible for throwing away this "understanding" of generic classes, in a process we call type erasure. All happens at compile time.

Note: Contrary to beliefs of majority of Java developers, it is possible to keep compile-time type information and retrieve this information at runtime, despite in a very restricted way. In other words: Java does provide reified generics in a very restricted way.

Regarding type erasure

Notice that, at compile-time, the compiler has full type information available but this information is intentionally dropped in general when the byte code is generated, in a process known as type erasure. This is done this way due to compatibility issues: The intention of language designers was providing full source code compatibility and full byte code compatibility between versions of the platform. If it were implemented differently, you would have to recompile your legacy applications when migrating to newer versions of the platform. The way it was done, all method signatures are preserved (source code compatibility) and you don't need to recompile anything (binary compatibility).

Regarding reified generics in Java

If you need to keep compile-time type information, you need to employ anonymous classes. The point is: in the very special case of anonymous classes, it is possible to retrieve full compile-time type information at runtime which, in other words means: reified generics. This means that the compiler does not throw away type information when anonymous classes are involved; this information is kept in the generated binary code and the runtime system allows you to retrieve this information.

I've written an article about this subject:

https://rgomes.info/using-typetokens-to-retrieve-generic-parameters/

A note about the technique described in the article above is that the technique is obscure for majority of developers. Despite it works and works well, most developers feel confused or uncomfortable with the technique. If you have a shared code base or plan to release your code to the public, I do not recommend the above technique. On the other hand, if you are the sole user of your code, you can take advantage of the power this technique delivers to you.

Sample code

The article above has links to sample code.

Solution 3

If you have a field that is a generic type, its type parameters are compiled into the class.

If you have a method that takes or returns a generic type, those type parameters are compiled into the class.

This information is what the compiler uses to tell you that you can't pass a Box<String> to the empty(Box<T extends Number>) method.

The API is complicated, but you can inspect this type information through the reflection API with methods like getGenericParameterTypes, getGenericReturnType, and, for fields, getGenericType.

If you have code that uses a generic type, the compiler inserts casts as needed (in the caller) to check types. The generic objects themselves are just the raw type; the parameterized type is "erased". So, when you create a new Box<Integer>(), there is no information about the Integer class in the Box object.

Angelika Langer's FAQ is the best reference I've seen for Java Generics.

Solution 4

Generics in Java Language is a really good guide on this topic.

Generics are implemented by Java compiler as a front-end conversion called erasure. You can (almost) think of it as a source-to-source translation, whereby the generic version of loophole() is converted to the non-generic version.

So, it's at compile time. The JVM will never know which ArrayList you used.

I'd also recommend Mr. Skeet's answer on What is the concept of erasure in generics in Java?

Solution 5

Type erasure occurs at compile time. What type erasure means is that it will forget about the generic type, not about every type. Besides, there will still be metadata about the types being generic. For example

Box<String> b = new Box<String>();
String x = b.getDefault();

is converted to

Box b = new Box();
String x = (String) b.getDefault();

at compile time. You may get warnings not because the compiler knows about what type is the generic of, but on the contrary, because it doesn't know enough so it cannot guarantee type safety.

Additionally, the compiler does retain the type information about the parameters on a method call, which you can retrieve via reflection.

This guide is the best I've found on the subject.

Share:
102,758

Related videos on Youtube

Radiodef
Author by

Radiodef

I'm really a musician, I guess, but lately I've been thinking about pursuing programming as a career instead. I took a few programming classes in high school, but otherwise I've just studied it as a hobby off and on. I know Java the best, but I also know some C++, JavaScript and Objective-C. The most substantial program I've written is Epsilon, a text-based calculator, written in Java. Block Breaker (a Breakout clone) is also cool. Along with computer programming, I've also studied math, philosophy, psychology and Japanese online (all to varying degrees), so I care a great deal about the quality of educational resources available on the internet. Below are some answers of mine which I'm particularly fond of, either because I think they're useful or just because they took a lot of work: How do I use audio sample data from Java Sound? Second order generics seem to behave differently than first order generics (explanation of Java capture conversion) Consumer&lt;T&gt; mapped Class&lt;T&gt; in HashMap Where is the Java Swing counterpart of “GetMessage()” loop? How to get annotation from overridden method in Java? (method override testing with reflection) I think all of my most upvoted answers are fine, but upvotes are really just an indication of the question's popularity, so they don't always correspond to what's the most interesting or challenging. If I've left a negative comment on your answer, please just address the issue and notify me when you're done. When I think issues that I've pointed out have been adequately addressed, I generally delete my comments so it looks like the conversation never took place. I believe this is how most criticisms should happen on the site. Just fix the problem and move on.

Updated on June 04, 2021

Comments

  • Radiodef
    Radiodef almost 3 years

    I read about Java's type erasure on Oracle's website.

    When does type erasure occur? At compile time or runtime? When the class is loaded? When the class is instantiated?

    A lot of sites (including the official tutorial mentioned above) say type erasure occurs at compile time. If the type information is completely removed at compile time, how does the JDK check type compatibility when a method using generics is invoked with no type information or wrong type information?

    Consider the following example: Say class A has a method, empty(Box<? extends Number> b). We compile A.java and get the class file A.class.

    public class A {
        public static void empty(Box<? extends Number> b) {}
    }
    
    public class Box<T> {}
    

    Now we create another class B which invokes the method empty with a non-parameterized argument (raw type): empty(new Box()). If we compile B.java with A.class in the classpath, javac is smart enough to raise a warning. So A.class has some type information stored in it.

    public class B {
        public static void invoke() {
            // java: unchecked method invocation:
            //  method empty in class A is applied to given types
            //  required: Box<? extends java.lang.Number>
            //  found:    Box
            // java: unchecked conversion
            //  required: Box<? extends java.lang.Number>
            //  found:    Box
            A.empty(new Box());
        }
    }
    

    My guess would be that type erasure occurs when the class is loaded, but it is just a guess. So when does it happen?

  • Rogério
    Rogério over 14 years
    No, even in the use of a generic type there may be metadata available at runtime. A local variable is not accessible through Reflection, but for a method parameter declared as "List<String> l", there will be type metadata at runtime, available through the Reflection API. Yep, "type erasure" is not as simple as many people think...
  • Jon Skeet
    Jon Skeet over 14 years
    @Rogerio: As I replied to your comment, I believe you're getting confused between being able to get the type of a variable and being able to get the type of an object. The object itself doesn't know its type argument, even though the field does.
  • Rogério
    Rogério over 14 years
    Of course, just looking at the object itself you can't know that it's a List<String>. But objects don't just appear from nowhere. They are created locally, passed in as a method invocation argument, returned as the return value from a method call, or read from a field of some object... In all these cases you CAN know at runtime what the generic type is, either implicitly or by using the Java Reflection API.
  • Jon Skeet
    Jon Skeet over 14 years
    @Rogerio: How do you know where the object has come from though? If you have a parameter of type List<? extends InputStream> how can you know what type it was when it was created? Even if you can find out the type of the field the reference has been stored in, why should you have to? Why should you be able to get all the rest of the information about the object at execution time, but not its generic type arguments? You seem to be trying to make type erasure out to be this tiny thing which doesn't affect developers really - whereas I find it to be a very significant problem in some cases.
  • Rogério
    Rogério over 14 years
    But type erasure IS a tiny thing which doesn't really affect developers! Of course, I can't speak for others, but in MY experience it never was a big deal. I actually take advantage of runtime type information in the design of my Java mocking API (JMockit); ironically, .NET mocking APIs seem to take less advantage of the generic type system available in C#.
  • Richard Gomes
    Richard Gomes almost 12 years
    @will824: I've massively improved the answer and I've added a link to some test cases. Cheers :)
  • Dzmitry Lazerka
    Dzmitry Lazerka over 11 years
    Actually, they didn't maintain both binary and source compatibility: oracle.com/technetwork/java/javase/compatibility-137462.html Where can I read more about their intention? Docs say that it uses type erasure, but doesn't say why.
  • Yann-Gaël Guéhéneuc
    Yann-Gaël Guéhéneuc about 11 years
    Actually, it is the formal generic type of the fields and methods that are compiled into the class, i.e., typicall "T". To obtain the real type of the generic type, you must use the "anonymous-class trick".
  • Yann-Gaël Guéhéneuc
    Yann-Gaël Guéhéneuc about 11 years
    @Richard Indeed, excellent article! You could add that local classes work too and that, in both cases (anonymous and local classes), information about the desired type argument is kept only in case of direct access new Box<String>() {}; not in case of indirect access void foo(T) {...new Box<T>() {};...} because the compiler does not keep type information for the enclosing method declaration.
  • Richard Gomes
    Richard Gomes over 4 years
    I've fixed a broken link to my article. I'm slowly de-googling my life and reclaiming my data. :-)