10

For the following conditionals:

if (a != null && a instanceof A)

or

if (a instanceof A)

Is there any advantage (for example, performance-wise) to check for null first? Both conditionals should be equal in result.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Dmitriy
  • 131
  • 1
  • 2
  • 5

5 Answers5

23

No advantage whatsoever, you can just do with

if(a instanceof A) {}

this will return evaluate to false if a is null

naikus
  • 24,302
  • 4
  • 42
  • 43
  • 6
    +1 JLS: 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. http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#80289 – barrowc Jul 25 '10 at 05:29
  • I've been programming Java since '97 and this was news to me. Thanks, +1! – Carl Smotricz Jul 25 '10 at 06:16
4

The JVM has to check for null itself. Not much point in doing it twice.

user207421
  • 305,947
  • 44
  • 307
  • 483
3

if(a instanceof A) is enough.

The if(a!=null && expr) pattern is used when expr will throw a NullPointerException if a is null. a instanceof A doesn't throw a NPE and just returns false if a is null.

samitgaur
  • 5,601
  • 2
  • 29
  • 33
0

The instanceOf operator does not need explicit null checks, as it does not throw a null pointer exception 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(a != null && a 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(a instanceof ArrayList){                     //Correct way
                      System.out.println("In if block");
             }
                else
              {
                     System.out.println("In else block");
             }
         }
Nikhil Kumar
  • 2,618
  • 3
  • 21
  • 24
  • imho: Absolutely no reason in your example to use instanceof when all your doing with it is `System.out::println` . Since instanceof also verifies type, then its useful to use that for your null check only if your going to need that type verification for some reason, such as to force a return type from the method. – djangofan Jun 08 '18 at 21:55
0

I seriously doubt that that there is any benefit to checking for null first. It is up to how the VM implements the instancof check... odds are it does the if itself as the first part of the code.

TofuBeer
  • 60,850
  • 18
  • 118
  • 163
  • 1
    Its not upto the VMs. Its the lang spec, the "null type" has one value: the null reference. The null reference is of type "null type" and hence the above check returns false for any other type – naikus Jul 25 '10 at 05:34
  • the VM has to implement the code though - and likely the first thing it does is check for null - so there is no performance gain by putting the check in. The RESULT is specified. How it achieves the result is not. – TofuBeer Jul 25 '10 at 06:11
  • @TofuBeer The behaviour is *specified.* It *must* check for null. It isn't up to an implementation in any way. – user207421 Aug 17 '15 at 10:25
  • Yes it must check that it is null. But it doesn't have to be the first thing it does... – TofuBeer Aug 21 '15 at 03:51