It is complicated...
For any type variable T
, the spec says http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.4
Every type variable ... has a bound. If no bound is declared for a type variable, Object is assumed.
One would think that it's true for wildcard too, and ?
should just be a shorthand for ? extends Object
.
Yet searching through the spec, there is no evidence at all that a wildcard must have an upper bound (or lower bound). The "unbounded" ?
is treated consistently distinctly from bounded wildcards.
We could deduce from subtyping rules, that List<?>
and List<? extends Object>
are subtypes of each other, i.e., they are basically the same type.
But the spec treats them separately nevertheless. For example http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.7 List<?>
is a reifiable type, but List<? extends Object>
is not, which means
// ok
List<?>[] xx = {};
// fail
List<? extends Object>[] yy = {};
// ok
boolean b1 = (y instanceof List<?>);
// fail
boolean b2 = (y instanceof List<? extends Object>);
I don't understand why though. It seems perfectly fine to say a wildcard must have an upper bound and a lower bound, default to Object
and null type
.