Why output is True? Is it something based on ASSOCIATIVITY property of "==" operator? | SoloLearn: Learn to code for FREE!

+1

Why output is True? Is it something based on ASSOCIATIVITY property of "==" operator?

As because of associative properties of relational operator x will increment and it become 2 and when it will compare with another x whose value is now 2...it will throw "True". IF I AM WRONG PLEASE CORRECT ME... THANKS! int x = 1; System.out.println(++x==x); //Output : True

3/25/2020 4:34:41 AM

Tushar

5 Answers

New Answer

+2

hi, it's probably because when ++x apply the x value change directly to +1+1=2 so he doesn't compare with another x, but with the same ++x change value of x during the execution of the code (not at the end)

+1

First, (++x) made the value of x to be 2 (increased by 1). Then, it compares it to the value of x (which is now 2) so it returns True (2==2).

+1

Oneill Thanks...

0

Zaid Al-husseini Okay that's one really helped...but The thing I want to know is which one compiler will execute first.. "x" or "++x" ? If x then situation will be something 2==1 hence, False....So how I will decide which one will execute first...based on ASSOCIATIVITY property of relational operator..if I execute it will execute from left to right...hence it will give "TRUE"

0

Tushar In java Expressions are evaluated from left to right. in ++x==x, ++x is gets the stack, it has high presidence than == so first ++x is evaluated, then now x=2, and it replaced by next value of stack variable x by 2 Next x==x takes place 2==2 return true.. [So ++x, x, x==x sequence of evaluations] Hope this helps you..