Got any code like this?
Certainly the JDK does - at least, in JDK 1.4.2, PlainSocketImpl.java
has a bunch of these.
But since instanceof
expressions return false
if the value of the relational expression is null (see JLS 2nd Ed, 15.20.2), you can simply reduce that code to:
Here's how the bytecode looks with the unnecessary null check:
and without it:
A nice little savings there - so your classfiles get smaller and your code runs a bit faster!
Of course, there's a PMD rule that checks for this. And you'll find many more code analyses of this sort in my recent book, PMD Applied. Enjoy!