Capturing wildcards in Java generics

前端 未结 1 1512
你的背包
你的背包 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.

    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)
    提交回复
    热议问题