# Reorder String Equality Check

# Properties

Property Value
Rule ID StringLiteralEqualityCheck
First seen in jSparrow version 2.0.0
Minimum Java version 1.1
Remediation cost 10 min
Links S1132

# Description

To avoid NullPointerExceptions , it is recommended to put string literals in the left-hand-side of equals() or equalsIgnoreCase() when checking for equality.

# Benefits

This order prevents NullPointerExceptions from being raised, as a string literal can never be null by definition, and therefore makes the code less prone to errors. An example can be seen below.

# Code Changes

Pre

getClass().getName().equals("Foo");

Post

"Foo".equals(getClass().getName());

Automatic Application of This Rule

The automatic application of this rule is supported in the following jSparrow versions:

# Tags

1
default
You & jSparrow
default

Hey there! May I help you? 😊