+ 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.
1 Answer
+ 2
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 !! đ