org.sonar.l10n.java.rules.squid.S1132.html Maven / Gradle / Ivy
It is preferable to place string literals on the left-hand side of an equals()
or equalsIgnoreCase()
method call.
This prevents null pointer exceptions from being raised, as a string literal can never be null by definition.
The following code:
String myString = null;
System.out.println("Equal? " + myString.equals("foo")); // Noncompliant - will raise a NPE
System.out.println("Equal? " + (myString != null && myString.equals("foo"))); // Noncompliant - null check could be removed
should be refactored into:
System.out.println("Equal?" + "foo".equals(myString)); // Compliant - properly deals with the null case
© 2015 - 2025 Weber Informatics LLC | Privacy Policy