51

When I compile and run this code:

public class Testing {
    public static void main(String... args) {
        Object obj = null;
        if (obj instanceof Object) {
            System.out.println("returned true"); 
        } else {
            System.out.println("returned false"); 
        }
        System.out.println(" " + obj instanceof Object);
    }
}

I get this on the command line:

C:\Users\xxxxxx\Desktop>java Testing
returned false
true

Shouldn't "null instanceof someType" always return false?

Infiniteh
  • 634
  • 5
  • 8

3 Answers3

102

This:

" " + obj instanceof Object

is evaluated as:

(" " + obj ) instanceof Object

and " " + obj is indeed a non-null string which is an instance of Object.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • 23
    This is a good lesson in "more parens are better than fewer when the operator precedence isn't blindingly obvious." `" " + (obj instanceof Object)` would have printed out the value you expected. – yshavit Sep 06 '12 at 09:05
  • 2
    Good point. This is the kind of counter-intuitive behaviour you can study in _Java Puzzlers_ from Joshua Block and Neal Gafter. – Alexandre Dupriez Sep 06 '12 at 09:09
  • @yshavit ... or use `String.valueOf(Object)` and not `"" + ...` when you want the String representation. – Fabian Barney Sep 06 '12 at 09:10
  • 1
    I just realized and refreshed and saw your answer :D – Infiniteh Sep 06 '12 at 09:10
  • 2
    If you got it by yourself it's even better :) – MByD Sep 06 '12 at 09:11
  • 3
    @FabianBarney True, but the operator precedence bit applies more generally than just to this case (as does `String.valueOf`, of course). For instance, maybe you wanted to output a debug message, `"obj is a Foo? " + obj instanceof Foo`. Boolean logic is another good example: what does `a || b ? c : d` mean? Is it `(a || b) ? c : d` or `a || (b ? c : d)`? Parens (or better yet, named vars!) make that code a lot less tricky. – yshavit Sep 06 '12 at 09:16
  • @yshavit, best thing is to learn and use debugger and step-in. (One would see the creation of the StringBuilder and the String). Although operator precedence MUST be known by heart by any programmer. – bestsss Sep 06 '12 at 15:52
  • 1
    @bestsss I actually disagree on that last bit. If someone asked me what `a || b ? c : d` means, I'd tell them it means they should put in parens so that I don't have to memorize arcane sections of the JLS. :) – yshavit Sep 06 '12 at 16:32
  • @yshavit, the result type of the operation is the same as `c` and `d`'s type. Indeed if all a,b,c and d are booleans it'd be confusing from 1st sight. Adding parenthesis is strongly advisable since it improves the readability, yet not knowing the result is no excuse. Ternary operator has lowest (beside assignment) priority (in all languages that support it and I can think of, so it's not just the JLS) – bestsss Sep 06 '12 at 16:46
  • Thanks God, in Lisp, we don't have to think about operators precedence. – Chiron Sep 12 '12 at 23:24
2

In the last System.out.println, the " " + obj evaluates first and the result, which is a String is checked for the instanceof Object and the result is printed.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
2

In (" " + obj) is the part which gets evaluated first so its no more null after parenthesis. So it is the instance of Object.

Also refer the below link so your concept will be clear.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

mevada.yogesh
  • 1,118
  • 3
  • 12
  • 35