Understanding wildcards in Java generics

后端 未结 7 1642
不思量自难忘°
不思量自难忘° 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:57

    The reference "b" is declared as a List, that is, a "List of something I don't know yet". You can assign pretty much any implementation to this reference, like a List. This is why it is forbidden to add anything to the lists through this reference.

提交回复
热议问题