Hi Nivas,
Please read carefully the posts by Dan. It is in the end of the
thread as suggested by
Hari.
The important thing Dan Said
The reason for the difference is the fact that the JVM processes the expression from left to right. The JVM can not look at the entire expression as we can. While a human can start the evaluation of an expression from the inner most parenthesis at any point in the expression--left, right, or center--the JVM must start parsing the expression on the left and work toward the right. therefor in your example
int a = 1
a += ++a + a++
I think what JVM might do is
a = a + ++a + a++
JVM just start replacing the values
a = 1 + then it finds a uanry operator it changes its value to 2 and then another additive operator and 2 then another additive operator
a = 1 + 2 + 2 = 5
Lets try another example
Let us evaluate as we human being will do to to the above code
we will first evaluate ++z in the end
z += z++ + x + y/4 ////now z is 4
z += 4 + x + y/4 //// now z is 5
z += 4 + 17 + 60/4
z = 5 + 36
z = 41
which is
INCORRECT. It is because we human can see the expressions at one time but it is not the case with the compilers they have to parse in a a order that is from either left to right or right to left.
lets see how the JVM evaluates
z = z + z++ + x + y / ++z /////line 1
z = 3 + 3 + 17 + 60 / 5 /////line 2
z = 35
In line 1 JVM first pareses it left to right
If you notice after line 2 it still follows the operator precedence first it evaluated the multiplicatibe operator (/) and then the additve operator.
I hope it will help