Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between explicitly putting the type into the diamond operator vs letting java figure it out?

Tags:

java

generics

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.

like image 353
Steve P. Avatar asked Jun 10 '13 21:06

Steve P.


1 Answers

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

like image 161
JB Nizet Avatar answered Sep 28 '22 18:09

JB Nizet