问题描述
是什么在一个ArrayList的以下两个声明的区别?
What is the difference in the two following declarations of an ArrayList?
ArrayList<Integer> nunbers = new ArrayList<Integer>();
VS
ArrayList<Integer> nunbers = new ArrayList<>();
就是其中之一pferred比其他$ P $?
Is one of them preferred over the other?
推荐答案
第二个拥有类型参数的 ,这是在Java 7中的新事物&LT;&GT;
被称为的。
The second one has its type parameter inferred, which is a new thing in Java 7. <>
is called "the diamond".
另外请注意,<一href=\"http://www.angelikalanger.com/GenericsFAQ/FAQSections/TechnicalDetails.html#What%20is%20type%20argument%20inference%20in%20general?\">type推论本身并不是新的Java中,但推断它被实例化泛型类的能力是新的。
Also note that type inference itself is not new in Java, but the ability to infer it for the generic class being instantiated is new.
这版本的Java SE 7之前,编译器能够推断出通用的构造,类似于通用方法的实际类型参数。然而,在Java SE 7的编译器和后可以推断出泛型类的实际类型参数被实例化,如果你使用的金刚石(小于>)。
我想说的第二个可能是preferred只要你能确保code只需要在Java 7的运行,因为它是更清晰,不仅减少了冗余信息。
I'd say the second one is probably preferred as long as you can make sure the code only needs to run on Java 7, since it is clearer, and only reduces redundant information.
这篇关于ArrayList的&LT;&GT; VS的ArrayList&LT;整数GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!