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

Why You Should Tune Code Before Your Garbage Collector

Asking multiple AI to optimise the same code

Demystifying Java Object Sizes: Compact Headers, Compressed Oops, and Beyond