Why does this use of Generics not throw a runtime or compile time exception?

  • A+
Category:Languages

I've got a method in a class that has a return type specified by use of a generic.

public class SomeMain {    public static void main(String[] args) {      Foo<Integer> foo = new Foo<Integer>();     System.out.println(foo.getFoo()); // Works, prints out "Foo"    }    public static class Foo<E>  {     public E getFoo() {       return (E) "Foo";     }   } } 

With the generic return type, I assumed the return in the above example would evaluate to:

return (Integer) "Foo";  // Inconvertible types !!! 

Instead a String is returned and printed correctly.

I get a compilation error if I change the call to be:

String fooString = foo.getFoo(); // Compile error, incompatible types found System.out.println(fooString); 

What am I missing to help me understand what's going on here and why the original version didn't result in a compilation error.

 


This is because overload resolution resolved your println call to println(Object), since there is no println(Integer).

Keep in mind that Java's generics are erased at runtime. And casts like (E) "Foo" are removed, and are moved to call site. Sometimes this is not necessary, so things are casted to the right type only when needed.

In other words, no casts are performed inside getFoo. The language spec supports this:

Section 5.5.2 Checked Casts and Unchecked Casts

  • The cast is a completely unchecked cast.

    No run-time action is performed for such a cast.

After erasure, getFoo returns Object. And that gets passed into println(Object), which is perfectly fine.

If I call this method and pass foo.getFoo, I will get an error:

static void f(Integer i) {     System.out.println(i); } // ... f(foo.getFoo()); // ClassCastException 

because this time it needs to be casted.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: