difference between Class and Class<?>

Igor Ignatyev igor.ignatyev at oracle.com
Thu Apr 18 14:36:25 PDT 2013


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
-- 
Best regards,
Igor Ignatyev
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Test.java
Type: text/x-java
Size: 287 bytes
Desc: not available
Url : http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20130419/77bd8589/Test.java 


More information about the compiler-dev mailing list