Consider the following code:
import java.util.ArrayList;
import java.util.List;
public class UnboundedWildcardProblem {
public static void main(String[] args) {
List<?> a = new ArrayList();
List<? extends Object> b = new ArrayList();
}
}
The creation of List<?>
doesn't produce any warnings, but the creation of List<? extends Object>
produces an unchecked warning:
Warning: java: unchecked conversion
required: java.util.List<? extends java.lang.Object>
found: java.util.ArrayList
I searched for possible reasons and found some discussions:
List<?> vs List<? extends Object>
What's the difference between <?> and <? extends Object> in Java Generics?
But after reading these discussions it seems to me that List<?>
and List<? extends Object>
are the same thing.
So, what is the reason for such compiler behaviour?
EDIT:
The single flag used for compilation was an Xlint:unchecked flag.
The creation of
List<?>
doesn't produce any warnings, but the creation ofList<? extends Object>
produces an unchecked warning
In fact, if you compile your code with the -Xlint:all
or just Xlint
option, you would get a warning for the List<?> a = new ArrayList();
statement, something like:
warning: [rawtypes] found raw type: ArrayList
List<?> a = new ArrayList();
^
missing type arguments for generic class ArrayList<E>
where E is a type-variable:
E extends Object declared in class ArrayList
However, the reason why you're not getting an unchecked warning per se is because the type List<?>
contains only unbounded wildcards as mentioned in this section from the Java Language Specification:
There is an unchecked conversion from the raw class or interface type (§4.8) G to any parameterized type of the form
G<T1,...,Tn>
....
Use of an unchecked conversion causes a compile-time unchecked warning unless all type arguments Ti (1 ≤ i ≤ n) are unbounded wildcards (§4.5.1), or the unchecked warning is suppressed by the
SuppressWarnings
annotation (§9.6.4.5).
However in this section, it is mentioned:
The wildcard
? extends Object
is equivalent to the unbounded wildcard?
.
which shows a subtle inconsistency in the compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With