0

When I simply write the code

String x = null;

System.out.println(x instanceof Object);

The output is false, as expected.

But when I try to write it slightly different, namely as:

public class Main {
        public static void main(String[] args) {
        String x = null;
        System.out.println("Result is: " + x instanceof Object);
    
  }
}

I get true as output. Why?

x instanced Object should still be false, no? I would like to get an output like

Result is: false
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • Does this answer your question? [What is the 'instanceof' operator used for in Java?](https://stackoverflow.com/questions/7313559/what-is-the-instanceof-operator-used-for-in-java) – Turamarth Jan 31 '22 at 08:00
  • 1
    Put braces around the `x instanceof Object` and try again. – deHaar Jan 31 '22 at 08:01
  • @deHaar no it doesn't work like that. I want to print like this -Result is:: false where false is the value of x instanced Object. Instead my codes prints true...which I don't understand why – Aditya Patel Jan 31 '22 at 08:11
  • @Turamarth No it doesn't. I want it to print like this - Result is:: false where false is the value of x instanced Object. Instead my codes prints true...which I don't understand why – Aditya Patel Jan 31 '22 at 08:13
  • @maloomeister I am not assuming ...since string x is pointing to null... x instance of Object is false for a null object. That is why it should be false...if I simply print System.out.println(x instanceof Object); it will come false...but when I try to format it the way I did in my question I get true as output...which is confusing. I know I am doing some silly mistake as I am a newbie and learning but I am not able to figure out my mistake – Aditya Patel Jan 31 '22 at 08:18
  • @AdityaPatel So did you actually put braces around like it was suggested? -> `System.out.println("Result is:: " + (x instanceof Object));` You say that you did, but it seems like you didn't, otherwise you would get the results you expect. – maloomeister Jan 31 '22 at 08:20
  • 2
    Downvoted and voting to close because you have not explained **why** you think it should be `false`. Because of that, it is impossible to answer your question without guessing. It is lacking details and is unclear, unfortunately. – Zabuzard Jan 31 '22 at 08:20
  • Good edit. Reversing my actions. – Zabuzard Jan 31 '22 at 08:22

1 Answers1

10

Why is it true?

Java evaluates your code in a different order than you might expect. When you write

System.out.println("Result is: " + x instanceof Object);

what Java executes is:

  1. String concatenation "Result is: " + x resulting in the string "Result is: null, then
  2. checking that string against instanceof Object, so "Result is: null" instanceof Object which is obviously true.

I.e. it executes it as if you would put paranthesis like this:

System.out.println(("Result is: " + x) instanceof Object);

This is due to the operator precedence rules in Java, which you can read more about here: Operators

You can see that addition + binds stronger than instanceof:

Precedence table

How to get the expected output?

In order to get the expected output, you have to make the order explicit by using paranthesis as follows:

System.out.println("Result is: " + (x instanceof Object));
// Result is: false

alternatively, split the expression into multiple by using some variables:

boolean isObject = x instanceof Object;
System.out.println("Result is: " + isObject);

Arguably also more readable.

As a general note, never rely on operator precedence rules (unless maybe * and + in a math context), otherwise you will only confuse readers.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82