Difference between LinkedList<?> and LinkedList<Object> [duplicate]

女生的网名这么多〃 提交于 2019-12-04 07:11:12

问题


Is there any difference between LinkedList< ? > and LinkedList< Object > in Java?


回答1:


This passes compilation:

LinkedList<?> list1 = new LinkedList<String> ();

This doesn't:

LinkedList<Object> list2 = new LinkedList<String> ();

i.e. a LinkedList<?> variable can be assigned any LinkedList<SomeType>. A LinkedList<Object> variable can only be assigned a LinkedList<Object> (or a raw LinkedList, which is not advised to use).

On the other hand the following add:

LinkedList<?> list1 = new LinkedList<String> ();
list1.add("x");

doesn't pass compilation, while the following does:

LinkedList<Object> list2 = new LinkedList<Object> ();
list2.add("x");


来源:https://stackoverflow.com/questions/44385926/difference-between-linkedlist-and-linkedlistobject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!