Why can not I add an object of type Object into a List<?>?

后端 未结 5 1511
忘了有多久
忘了有多久 2020-12-21 12:03

As far as I understand a List is definds as a list of some specific , yet unknown type . So whatever is the parameter type of this List

5条回答
  •  Happy的楠姐
    2020-12-21 12:34

    You are making a confusion between a List and a List or List.

    With a List, you can put anything into it but List doesn't mean that this is a List or that it will (necessarily) receive a List.

    It could receive a List, in this case adding an Object would be permissible; however, it could also receive anything else like a List, a List or a List. Obviously, you cannot add an Object to a List, a List or a List.

    As the compiler doesn't remember the type of the object between instructions, adding an Object will always be illegal even if you set the List to a List because the compiler won't remember that you have set it to a List.

    提交回复
    热议问题