What is the right position of literals in String Comparison?

unj2 picture unj2 · Jun 23, 2010 · Viewed 12k times · Source

I have

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

but PMD says

Position literals first in String comparisons

Answer

Adam picture Adam · Jun 23, 2010

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