Home » Java » Is null check needed before calling instanceof

Is null check needed before calling instanceof

Posted by: admin November 2, 2017 Leave a comment

Questions:

Will null instanceof SomeClass return false or throw a NullPointerException?

Answers:

No, a null check is not needed before using instanceof.

The expression x instanceof SomeClass is false if x is null.

From the Java Language Specification, at http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2

“At run time, the result of the
instanceof operator is true if the
value of the RelationalExpression is
not null
and the reference could be
cast (§15.16) to the ReferenceType
without raising a ClassCastException.
Otherwise the result is false.”

So if the operand is null, the result is false.

Questions:
Answers:

Using a null reference as first operand with instanceof, returns false.

(It takes 1 minute to try it)

Questions:
Answers:

Very good question indeed. I just tried for myself.

public class IsInstanceOfTest {

    public static void main(final String[] args) {

        String s;

        s = "";

        System.out.println((s instanceof String));
        System.out.println(String.class.isInstance(s));

        s = null;

        System.out.println((s instanceof String));
        System.out.println(String.class.isInstance(s));
    }
}

Prints

true
true
false
false

JLS / 15.20.2. Type Comparison Operator instanceof

At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException. Otherwise the result is false.

API / Class#isInstance(Object)

If this Class object represents an interface, this method returns true if the class or any superclass of the specified Object argument implements this interface; it returns false otherwise. If this Class object represents a primitive type, this method returns false.

Questions:
Answers:

No, it’s not. instanceof would return false if its first operand is null.

Questions:
Answers:

Java literal null is not an instance of any class. Therefore it can not be an instanceof any class. instanceof will return either false or true therefore the <referenceVariable> instanceof <SomeClass> returns false when referenceVariable value is null.

Questions:
Answers:

The instanceof operator does not need explicit null checks, as it does not throw a NullPointerException if the operand is null.

At run time, the result of the instanceof operator is true if the value of the relational expression is not null and the reference could be cast to the reference type without raising a class cast exception.

If the operand is null, the instanceof operator returns false and hence, explicit null checks are not required.

Consider the below example,

public static void main(String[] args) {
         if(lista != null && lista instanceof ArrayList) {                     //Violation
                System.out.println("In if block");
         }
         else {
                System.out.println("In else block");
         }
}

The correct usage of instanceof is as shown below,

public static void main(String[] args) {
      
         if(lista instanceof ArrayList){                     //Correct way
                  System.out.println("In if block");
         }
            else {
                 System.out.println("In else block");
         }  
}