+ 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

25th Mar 2020, 4:34 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
5 ответов
+ 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)
25th Mar 2020, 4:45 AM
Oneill~Онеилл~奥尼尔~ओनील~اونیل~*‎
Oneill~Онеилл~奥尼尔~ओनील~اونیل~*‎ - avatar
+ 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).
25th Mar 2020, 4:47 AM
Zaid Al-husseini
Zaid Al-husseini - avatar
+ 1
Oneill Thanks...
25th Mar 2020, 5:19 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
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"
25th Mar 2020, 5:22 AM
I Am a Baked Potato
I Am a Baked Potato - avatar
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..
25th Mar 2020, 10:00 AM
Jayakrishna 🇮🇳