If I am trying to do something like this :
List<?> unknownList = new ArrayList<>();
then the code compiles and runs fine, but of which type the ArrayList
has created?
and after this line, If I have done like this :
unknownList.add("str"); //compilation error
It gives compilation error :
error: no suitable method found for add(String)
unList.add("str");
^
method List.add(int,CAP#1) is not applicable
(actual and formal argument lists differ in length)
method List.add(CAP#1) is not applicable
(actual argument String cannot be converted to CAP#1 by method invocation conversion)
method Collection.add(CAP#1) is not applicable
(actual argument String cannot be converted to CAP#1 by method invocation conversion)
where CAP#1 is a fresh type-variable:
CAP#1 extends Object from capture of ?
What is this error, and is it good to use the diamond-operator with wildcards? If YES then WHERE???
See Question&Answers more detail:os