Java中实例化List<T>时需不需要写明T?
怪我咯
怪我咯 2017-04-18 09:31:13
0
4
334
List<String> stringList = new ArrayList<String>();

List<String> stringList = new ArrayList<>();

这两种实例化stringList方式有什么不同?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(4)
Peter_Zhu

The second way of writing is the new feature of jdk7, generic inference. Based on the generic type when the previous variable is declared, the generic type instantiated later is inferred

刘奇

jdk 5 and below versions support List<String> stringList = new ArrayList<String>();
jdk 5+ and above versions all support

迷茫

The second writing method requires JKD7+. There is no difference between the two writing methods. The second writing method is just a little simpler.

Peter_Zhu

The second one is the syntactic sugar provided by JDK7, called Diamond Operator. See this article: http://www.javaworld.com/article/2074080/core-java/jdk-7--the-diamond-operator.html

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!