public static void main(String args)
sum=++e+e--;//e-- should b evaluated first
The above code prints
sum=22. But according to Java specification
e-- should be evaluated before
++e. So result should
sum=20 instead of
When you say
e-- should be evaluated first,
sum=++e+e--;//e-- should be evaluated first
you must be incorrect. For one thing
e-- doesn't change
e until the next statement (that is the post in post-decrement). Next, if you change your code to,
int sum = ++e + --e; System.out.printf("sum=%d%n", sum);
You will see that first
e is pre-incremented then added to a pre-decrement of
e (in that order) because the sum is
Your expected result (20) can be obtained with
int e = 11; int sum = --e + e++; System.out.printf("sum=%d%n", sum);
int e = 9; int sum = ++e + e--; System.out.printf("sum=%d%n", sum);
I hope that helps.
Per your Java Tutorials link,
All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.
You can't just skim the table.