Java unbound wildcard generics

馋奶兔 提交于 2019-11-30 10:28:55

There are multiple advantages.

  • They won't produce compiler warnings like using the raw type would
  • They give more type safety. For example, consider if Foo was List instead. If you used List instead of List<?>, you could do this:

    myBar.getFoo("numbers").add("some string");
    

    even if the list was only supposed to contain Numbers. If you returned a List<?>, then you would not be able to add anything to it (except null) since the type of list is not known.

  • They document something completely different than a raw type, namely that Foo is typed on some unknown but specific type.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!