Java

Java

Made by DeepSource

Avoid using equals to compare against null JAVA-E0051

Bug risk
Critical
Autofix

Tests for null should not use the equals method. The '==' operator should be used instead.

Consider this string declaration:

String x = "foo";

Bad Practice

if (x.equals(null)) {
    doSomething();
}

If x is null in the above snippet, calling equals on it would result in a NullPointerException.

Recommended

if (x == null) {
    doSomething();
}

Since the == operator directly compares references, it does not have the same problem.

References