When we declare a List
as List<?> list = new ArrayList<>()
, we won't be able to add anything to list
since ?
is some unknown type, we can only read from a list of some unknown type. So is there is any chance that we need a declaration as List<?> list = new ArrayList<>()
?
Asked
Active
Viewed 47 times
0

user2018791
- 1,143
- 15
- 29
-
1The duplicate is not an *exact* duplicate, but you can see that it may be somewhat useful as a parameter, but you'll never **need** to use `List> list = new ArrayList<>();`, and you really *shouldn't* either, since it can only make it harder to write your code. You could write `List> list = getList();` to have content in the list, but in this case the only reason you would write so is if `getList();` returned a wildcard list, i.e. most likely was poorly designed. – Kayaman Sep 01 '16 at 09:34
-
When it is used as a parameter, what's the difference between `void foo(List list)` and `void foo(List> list)` ? – user2018791 Sep 01 '16 at 11:53
-
Well, `List>` won't give you a warning about using raw types, and of course you can't add to it. – Kayaman Sep 01 '16 at 12:25