新ArrayList< String>()与新ArrayList<>()之间的区别 [英] Difference between new ArrayList<String>() and new ArrayList<>()

查看:203
本文介绍了新ArrayList< String>()与新ArrayList<>()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我理解下面写的两行有什么区别,我知道两者都有效,但我想知道哪一行更好?

Please help me to understand what is the difference between two lines written below, I know both is valid but I want to know which one is better?

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

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


推荐答案


  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.

这篇关于新ArrayList&lt; String&gt;()与新ArrayList&lt;&gt;()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆