Sonar complaining “The diamond operator (”<>“) should be used”

  • A+
Category:Languages

So I have below line of code in my file

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

Now each time I run sonar scans it shows an error in above line saying I should use diamond operator instead. Now I understand that from Java7 compiler will automatically detect and supply the type of objects for ArrayList but my question is there a harm if I do it myself while declaring the class ?

Here is the rule link from Sonar website. I really don't understand the example they are supplying with this rule.

Is there any performance, efficiency or any other type of gain in changing the code to what Sonar is suggesting ?

 


is there a harm if I do it myself while declaring the class ?

No, it is not any harm in doing it this way, except for the extra verbosity added to the code. From the same rule link we can read that

Java 7 introduced the diamond operator (<>) to reduce the verbosity of generics code


Is there any performance, efficiency or any other type of gain in changing the code to what Sonar is suggesting ?

Except for the fact that you (and your team) will have a less verbose code, no, there is not.


Further readings:

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: