Java - Is new Stack<?>[N] equivalent to new Stack[N] for generic data type Stack<Item>?

 ̄綄美尐妖づ 提交于 2019-12-05 19:04:26

Yes, they are equivalent, except for the return type (one is Stack[], the other is Stack<?>[]).

Note that Stack[] can be assigned to Stack<String>[] without an explicit cast (you will just get an unchecked conversion warning), whereas it is an error to assign a Stack<?>[] to Stack<String>[] without a cast.

new Stack[N] uses a raw type which is not recommended in new code.

[ Turning my comment into a real answer ]

1) Mixing arrays and generic types should be avoided.

2) Instead, one can use Java Collection types, for example ArrayList when the need comes up to handle multiple "generic" objects, like in

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