difference between Class and Class<?>

Alex Buckley alex.buckley at oracle.com
Fri Apr 19 11:40:45 PDT 2013


Essentially the same scenario as 
http://bugs.sun.com/view_bug.do?bug_id=6400189. It led to a 
clarification in JLS7 that a method's return type, while not formally 
part of the method's signature, was indeed erased along with the signature.

Alex

On 4/19/2013 5:34 AM, Igor Ignatyev wrote:
> Hi.
>
> I noticed some strange thing in javac:
> if variable has type Class<?>, return type of the method 'A
> Class:getAnnotation(Class<A>)' will be 'A'.
> but if variable has raw-type Class, return type of the method will be
> 'j.l.a.Annotation' instead of 'A', and javac will end compilation with
> error: 'incompatible types'.
>
> can someone explain a reason of this behavior?
>
> $ cat ./Test.java
> class Test {
>     public static Deprecated m1(Class k) { return
> k.getAnnotation(Deprecated.class);  }
>     public static Deprecated m2(Class<?> k) { return
> k.getAnnotation(Deprecated.class); }
>     public static Deprecated m3(Class k) { return ((Class<?>)
> k).getAnnotation(Deprecated.class); }
> }
>
> $ javac Test.java
> Test.java:2: error: incompatible types
>     public static Deprecated m1(Class k) { return
> k.getAnnotation(Deprecated.class);  }
>                                                                  ^
>     required: Deprecated
>     found:    Annotation
> Note: Test.java uses unchecked or unsafe operations.
> Note: Recompile with -Xlint:unchecked for details.
> 1 error



More information about the compiler-dev mailing list