unj2 unj2 - 2 months ago 20
Java Question

What is the right position of literals in String Comparison?

I have

if (localName.equals("TaxName")) {


but PMD says

Position literals first in String comparisons

Answer

"TaxName".equals(localName) is better as if localName is null you won't get a null pointer exception.