Test if object implements interface

The instanceof operator does the work in a NullPointerException safe way. For example:

 if ("" instanceof java.io.Serializable) {
     // it's true
 }

yields true. Since:

 if (null instanceof AnyType) {
     // never reached
 }

yields false, the instanceof operator is null safe (the code you posted isn’t).

instanceof is the built-in, compile-time safe alternative to Class#isInstance(Object)

Leave a Comment