0

I just wonder if there is a good reason or practice of when to use != null instead of instanceof to check if something is null.

Is a bad practice to test if something is null with instanceof ?

For example:

View view = ((Activity) context).findViewById(viewID);
                                if (view instanceof  View) {
                                    listener.onView(view, viewID);
                                }

or

View view = ((Activity) context).findViewById(viewID);
                            if (view != null) {
                                listener.onView(view, viewID);
                            }

Shouldn't it works just as same?

wviana
  • 1,619
  • 2
  • 19
  • 47
  • 16
    If you want to check for nullity, why on earth *would* you use `instanceof`? – Jon Skeet May 08 '15 at 13:53
  • 1
    _"Is a bad practice to test if something is null with instanceof"_ **Yes**. It might not even work correctly. – Jonas Czech May 08 '15 at 13:54
  • @JonasCz I don't agree, why the double check is required. – prashant thakre May 08 '15 at 13:59
  • Perhaps: http://stackoverflow.com/questions/2950319/is-null-check-needed-before-calling-instanceof –  May 08 '15 at 14:01
  • Use `instanceof` if and only if you're checking whether something is an instance of a type. Even though it returns false for null (and theoretically `instanceof Object` equals `!= null`), there's no point to do that and it would even be less readable. – Bubletan May 08 '15 at 14:04
  • @JonSkeet I might use 'if (view != null)' or 'if (view instanceof View)' – wviana May 08 '15 at 14:04
  • @jdv I saw this question before make mine. But I was looking for a explanation of Why do it or why don't. – wviana May 08 '15 at 14:08
  • 1
    Yes, the related question is about why and when, not necessarily how. But the best answer shows that instanceof is a valid way to test for null, and some famous people may advocate it -- see the reference there for examples from Bloch. My advice is that code is read by two compilers: the JDK and the other humans reading your code. I tend to write for the latter over the former. –  May 08 '15 at 14:17

4 Answers4

4

That depends on what you are actually checking

  • If you just want to know whether the reference is null, use x == null and nothing else

  • If you want to know whether a particular reference is not null and points to an instance of type Foo you may use x instanceof Foo as it implies being not null

    But if the compile-time type of the reference is already Foo, you know that the instance is of type Foo when it is non-null, therefore the first bullet applies in this case; you just want to test for null. This is the case in your updated question’s example, the reference already has the compile-time type View.

  • If you want to know whether a type cast (Foo)x will succeed, you may use x == null || x instanceof Foo as the type cast will also succeed when x is null (however, you should think twice whether you really want to accept nulls, even if the type cast will be successful)

Community
  • 1
  • 1
Holger
  • 285,553
  • 42
  • 434
  • 765
  • Really good explanation. What do you think about the example code case ? – wviana May 08 '15 at 14:32
  • 1
    As said, the compile-time type is already `View`, so it should be a plain `null`-test using `… == null` as it’s guaranteed that non-`null` references point to instances of `View`. As a basic rule of thumb, if you don’t need a type cast, you usually don’t need an `instanceof`. – Holger May 08 '15 at 14:35
  • Do you know if more JVM instructions are executed to a 'instanceof' than '!= null' PS: I think it does (probably does), just to confirm the thoughts. – wviana May 08 '15 at 14:41
  • 1
    Well, you can encode a `null` test with a single [`ifnull`](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.ifnull) or `ifnonnull` instruction whereas an `instanceof` requires one [`instanceof`](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.instanceof) instruction, followed by either [`ifne`](http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.if_cond) or `ifeq`. But that’s irrelevant, at least in earlier JVMs, dynamic type checking was way more expensive than a simple `null` reference comparison. But it’s not that big anymore. – Holger May 08 '15 at 14:48
0

Null check is not needed at all to check before instanceof. If it's null then during check of instanceof it will return false.

Is a bad practice to test if something is null with instanceof ?

During runtime when you are not sure it can be null or not.just simply use instanceof it will return false in case of null object.

prashant thakre
  • 5,061
  • 3
  • 26
  • 39
0

I just wonder if there is a good reason or practice of when to use != null instead of instanceof to check if something is null.

Of course there's a reason, the !=null can be applied to all objects in Java, but instanceofcan't be applied to Primitive types it only checks if a given object is instance of a class, that's why we use !=null to check for nullity.

Is a bad practice to test if something is null with instanceof ?

You can use it, but why whould you use instanceof if it may cause problems with primitive types and if !=null can be applied to all objects.

EDIT:

In your case using if (view instanceof View) is much safer because you are using it to test if it's an instance of View and in the same time if it's not null.

And as you can see in "Here" you can see that you can use instanceof to check for nullity.

Community
  • 1
  • 1
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • 3
    A primitive like an int is not an object so neither comparing it with null or passing it to instanceof is not valid Java. So, it isn't a particularly good way to compare the use of either... –  May 08 '15 at 14:10
  • 3
    To add, `String` is not a primitive type. – Bubletan May 08 '15 at 14:11
  • What do you think about the case of the example? – wviana May 08 '15 at 14:11
  • The variable `view` has the declared compile-time type `View` and therefore can’t have another type. Decent IDEs will even warn you about an obsolete `instanceof` test then. – Holger May 08 '15 at 14:15
  • @Holger You are right I missed that, I corrected my answer. – cнŝdk May 08 '15 at 14:17
0

instanceof instruction is used to check if an object is an instance of a certain class.

So, using it to check null value simply makes no sense...


In your code snippets you are checking two different things:

  • in the first one you are checking if your object is an instance of View class
  • in the second one you are checking if the object is initialized

So they are not two ways to do the same thing...

Take a look: use of "Instance of" in java

Community
  • 1
  • 1
davioooh
  • 23,742
  • 39
  • 159
  • 250