Best Practice: Don't use equals(null)

Overview

Sometimes it is not clear when to use == or equals() however one situation where equals is never useful is with null.

Don't use equals(null)

From SerialRef
if(!object.equals(null)) {
The only class object can be is a SerialRef which doesn't implement equals() so a == comparison happens to be used. However this is not clear and a different implementation could be used which doesn't do this.

A better alternative would be
if(object != null) {
which is probably what was intended.

Comments

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Unusual Java: StackTrace Extends Throwable