且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

新ArrayList< String>()与新ArrayList<>()之间的区别

更新时间:2023-08-22 23:37:46


  1. 其中一个更好吗?

  1. Which one is better?

就个人而言,我喜欢使用第一个,因为我认为这对我来说更清楚,但这是你的个人偏好,其他人认为它是多余的。

Personally, I like using the first one as I think it is more clear to me, but it is your personal preference and others do think it is redundant.

有什么区别?

两条线之间没有区别,它们意味着在Java 7中也是如此(如果你的目标是Java 6使用第一个)。

There is no difference between the two lines, they mean the same thing in Java 7 (if you are targeting Java 6 use the first one).

此外,正如Elliott Frisch指出的(由于有用的澄清)由于类型擦除,没有办法区分这两个代码片段,因为它从两者中删除了泛型。当最初引入泛型类型时,这适用于JDK 5.

Also, as Elliott Frisch notes (thanks for the helpful clarification) due to type erasure, there is no way to differentiate between the two code snippets as it removes the generics from both. This works up to JDK 5, when generic types were first introduced.