difference between Class and Class<?>
Boaz Nahum
boaznahum at gmail.com
Fri Apr 19 07:43:38 PDT 2013
I'm not oracle professional, but I know JLS (4.8) says:
"
The type of a constructor
(§8.8<http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8>),
instance method
(§8.4<http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4>,
§9.4 <http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.4>),
or non-static field
(§8.3<http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3>)
M of a raw type C that is not inherited from its superclasses or
superinterfaces is the raw type that corresponds to the erasure of its type
in the generic declaration corresponding to C.
"
So in :
class C<T> {
<K> K getK(K k) { return k;}
}
And raw type of it:
C ar = new C<>();
Then all non static members become raw too, so you have:
Object getK(Object k);
This always surprises me. I understand that non static member method might
depend on C generic members:
class C<T> {
D<T> d;
<K> K getK(K k) { return d.getK();}
}
So if C erased you must also erase getK().
But I didn't find an example that break the type safety (how can d return
something that is related to K?)
Boaz
On Fri, Apr 19, 2013 at 3:34 PM, Igor Ignatyev <igor.ignatyev at oracle.com>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
> --
> Best regards,
> Igor Ignatyev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20130419/7b948872/attachment.html
More information about the compiler-dev
mailing list