equals
to compare against null
JAVA-E0051Tests for null should not use the equals
method. The '==' operator should be used instead.
Consider this string declaration:
String x = "foo";
if (x.equals(null)) {
doSomething();
}
If x
is null in the above snippet, calling equals
on it would result in a NullPointerException
.
if (x == null) {
doSomething();
}
Since the ==
operator directly compares references, it does not have the same problem.