将类型明确地放入钻石算子并让java找出它们之间有什么区别? [英] Is there a difference between explicitly putting the type into the diamond operator vs letting java figure it out?

查看:90
本文介绍了将类型明确地放入钻石算子并让java找出它们之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  MyWrapper< String> wrapper = new MyWrapper< String>(); 

vs initializing via:

  MyWrapper<字符串> wrapper = new MyWrapper<>(); 

是否有任何理由为什么要使用前者而不使用后者?我看到很多前者;我不确定是否仅仅是因为那是人们习惯的,或者你有这样想写的原因。

div>

后者仅在Java 7以后才可用。这就是为什么你经常看到前者。后者是相同的,并且更短。

Is there any difference between initialization via:

MyWrapper<String> wrapper = new MyWrapper<String>();

vs initialization via:

MyWrapper<String> wrapper = new MyWrapper<>();

Is there any reason why one would want to use the former over the latter? I see a lot of the former; I'm not sure if it's just because that's what people are used to, or there's a reason you'd want to write it that way.

解决方案

The latter is only available since Java 7. That's why you often see the former. The latter is equivalent, and shorter.

这篇关于将类型明确地放入钻石算子并让java找出它们之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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