0
List<?> l1 = new ArrayList<>();
List<? extends Object> l2 = l1;

no compile error here, however

List<?> l1 = new ArrayList<>();
List<? super Object> l2 = l1;

there is a compile error. It's so confused!

  • 1
    First start here: https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super Then you need to read some actual documentation. I found the explanations in *Learning Java* by O'Reilly to be pretty good on generics. The short answer is that nothing (?) can be the super class of `Object` so that one is obviously wrong. – markspace Mar 08 '20 at 02:28

1 Answers1

1

Try putting any type into the diamond.

List<?> l1 = new ArrayList<String>();

Now:

List<? extends Object> l2 = l1;

Yup, String extends Object.

List<? super Object> l2 = l1;

Nope, String is not a supertype of Object.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305