Understanding wildcards in Java generics

后端 未结 7 1650
不思量自难忘°
不思量自难忘° 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条回答
  •  北荒
    北荒 (楼主)
    2020-12-31 11:49

    It's because we cannot guarantee that Integer is a subtype of the parameter type "?".

    Look this:

    Object c = b.get(0);
    

    This is valid, as ? will always be an subtype from Object.

提交回复
热议问题