Is null check needed before calling instanceof?

JavaNullpointerexceptionNull

Java Problem Overview


Will null instanceof SomeClass return false or throw a NullPointerException?

Java Solutions


Solution 1 - Java

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

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

The Java 11 Language Specification expresses this concisely in section 15.20.2, "Type comparison operator instanceof". (Java 17 expresses this less concisely, after the introduction of instanceof patternmatching.)

> "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."

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

Solution 2 - Java

Using a null reference as the first operand to instanceof returns false.

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

Just as a tidbit:

Even (((A)null) instanceof A) will return false.


(If typecasting null seems surprising, sometimes you have to do it, for example in situations like this:

public class Test
{
  public static void test(A a)
  {
    System.out.println("a instanceof A: " + (a instanceof A));
  }
  
  public static void test(B b) {
    // Overloaded version. Would cause reference ambiguity (compile error)
    // if Test.test(null) was called without casting.
    // So you need to call Test.test((A)null) or Test.test((B)null).
  }
}

So Test.test((A)null) will print a instanceof A: false.)


P.S.: If you are hiring, please don't use this as a job interview question. :D

Solution 6 - Java

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");
       	 }  
}

Solution 7 - Java

  • null check is not needed before instanceof
  • null check is not needed after instanceof that validates to true

The following are null-safe:

if(couldbenull instanceof Comparable comp){
   return comp.compareTo(somethingElse);
}
//java < 14
if(couldbenull instanceof Comparable){
   return ((Comparable)couldbenull).compareTo(somethingElse);
}

Solution 8 - Java

No, a null check is not needed before calling instanceof. It always returns false if its value is null.

As per Java Language Specification for comparison using 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

Hence we can infer that java has something called null type also, and this null type is checked in instanceof operator which obviously returns false because it is expecting a specific type.

There are two kinds of types in the Java programming language: primitive types and reference types. As per Java Specification on types and value

> There is also a special null type, the type of the expression null, > which has no name. Because the null type has no name, it is impossible > to declare a variable of the null type or to cast to the null type. > The null reference is the only possible value of an expression of null > type. The null reference can always undergo a widening reference > conversion to any reference type.

From Java 14 onwards and esp. in LTS Java 17 we have an enhanced instanceof. We have pattern matching feature which performs casts after type comparisons.

Example

public static void main(String[] args) {
    Object testObject = "I am a string";
    List<Object> testList = null;
    if (testList instanceof List) {
        System.out.println("instance of list");
    } else {
        System.out.println("null type");
    }
    //Enhanced instanceof with type conversion - tested with JDK 17
    if (testObject instanceof String str) {
        System.out.println(str.toUpperCase());
    }
}

Output

null type
I AM A STRING

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohan L&#252;bckeView Question on Stackoverflow
Solution 1 - JavaAndy ThomasView Answer on Stackoverflow
Solution 2 - JavaBozhoView Answer on Stackoverflow
Solution 3 - JavaJin KwonView Answer on Stackoverflow
Solution 4 - JavaRoflcoptrExceptionView Answer on Stackoverflow
Solution 5 - JavaAttila TanyiView Answer on Stackoverflow
Solution 6 - JavaNikhil KumarView Answer on Stackoverflow
Solution 7 - JavaMarinos AnView Answer on Stackoverflow
Solution 8 - JavaTrisView Answer on Stackoverflow