Understanding wildcards in Java generics

后端 未结 7 1628
不思量自难忘°
不思量自难忘° 2020-12-31 11:07

I\'m not sure why the last statement in the following code is illegal. Integer should be a subtype of ?, so why can\'t I assign it to b

7条回答
  •  Happy的楠姐
    2020-12-31 11:50

    List means a list typed to an unknown type.This could be a List of Integer, String or XYZ class.

    Since you don't know what type of list is typed to, you can only read from the collection, and you can only treat the Objects read as being Object instance.

    Please go for super wildcard boundary if you want to insert elements in the wildcard generic collection.

提交回复
热议问题