I am used to java generics, e.g.
class B {}
class A<T extends B> {
T t;
T getT() {
return t;
}
}
Now, what exactly is the meaning in the following piece of code in this context?
class C {
A<?> a;
A<?> getA() {
return a;
}
}
The code compiles, but I wonder why the compile is fine with using A<?>
? I would expect the compiler to complain that A
must be used in the same way either on the attribute level (a1
) or on the class level (a2
):
class C<T extends B> {
A<? extends B> a1;
A<? extends B> getA1() {
return a1;
};
A<T> a2;
A<T> getA2() {
return a2;
}
}
So my guess is that A<?>
is only syntactic sugar, but the meaning is equal to A<? extends B>
? However, many answers on stack overflow regarding the notation Something<?>
means Something<? extends Object>
which then would mean that all those answers are wrong?
But it is not possible to assign a A<?>
typed variable to one of type A<? extends B>
, which makes clear that the compiler is unaware of the fact that it is impossible to create an A<X>
where X
is not a subclass of B
.
Theoretical, the A<?>
just means unknown type argument, but the compiler should still know that the type argument must be a subclass of B because of the type parameter definition of A. But it seems that the compiler does not take into account this? Why?