What is the point of the diamond operator (<>) in Java 7?

后端 未结 7 2388
渐次进展
渐次进展 2020-11-21 04:20

The diamond operator in java 7 allows code like the following:

List list = new LinkedList<>();

However in Java 5/6, I c

7条回答
  •  自闭症患者
    2020-11-21 05:14

    The point for diamond operator is simply to reduce typing of code when declaring generic types. It doesn't have any effect on runtime whatsoever.

    The only difference if you specify in Java 5 and 6,

    List list = new ArrayList();
    

    is that you have to specify @SuppressWarnings("unchecked") to the list (otherwise you will get an unchecked cast warning). My understanding is that diamond operator is trying to make development easier. It's got nothing to do on runtime execution of generics at all.

提交回复
热议问题