Capturing wildcards in Java generics

前端 未结 1 1511
你的背包
你的背包 2020-12-19 03:08

From this Oracle Java tutorial:

The WildcardError example produces a capture error when compiled:

public class WildcardE         


        
相关标签:
1条回答
  • 2020-12-19 03:48

    List<?> does mean list of object of unknown type, it's not the same as List<Object>.

    Because we don't know the type of elements in the list result of i.get(0) is considered by Java as Object, and you cannot add Object to List<?>. In case as your Java could be smarter, but in more complex code with <?> wildcards it's easy to make it no type safe.

    0 讨论(0)
提交回复
热议问题