Why does Java compiler prints a value, while a C++ compiler outputs another in the next expression? | SoloLearn: Learn to code for FREE!

+1

Why does Java compiler prints a value, while a C++ compiler outputs another in the next expression?

If we have: int x ; x = x + (x = 3); I know that what a C++ compiler would do first is to evaluate the expression from right to left, so the expression x = x + (x = 3) becomes x = 3 + 3. But, what about Java? I read that Java compiler uses right to left associaticity as well, but Java will print 4 in that expression. I'd like to know why this happens.

5/29/2020 9:08:18 AM

Sergio Enrique ☕

1 Answer

New Answer

+1

Bro, you have got the wrong information!! Java uses left to right order of evaluation. So , firstly variable x needs to be initialized else it will throw an error message. Therefore int x=0; x=x+(x=3); x=0+3 x=3 Hence, it will output 3. Hope u understood !! 😃