In the expression
a = a-- + a--;
you have a lot of sub-expressions that need to be evaluated before the whole of the expression is evaluated.
a = a-- + a--;
^^^ <= sub-expression 2
^^^ <= sub-expression 1
What's the value of sub-expression 1? It's the current value of the object a
.
What's the value of the object a
?
If the sub-expression 2 was already evaluated, value of object a
is 9, otherwise it is 10.
Same thing for sub-expression 2. Its value can be either 9 or 10, depending on whether sub-expression 1 was already evaluated.
The C compiler (don't know about Java) is free to evaluate the sub-expressions in any order
So let's say the compiler chose to leave the --
s for last
a = 10 + 10;
a--; /* 19 */
a--; /* 18 */
but on the next compilation the compiler did the --
s up front
/* value of sub-expression 1 is 10 */
/* value of sub-expression 2 is 9 */
a = 10 + 9; /* a = 9 + 10; */
or it could even save one of the a--
and use that for the final value of a
/* sub-expression 1 yields 10 and sets the value of `a` to 9 */
/* so yield that 10, but save the value for the end */
a = 10 + ???;
a = 18???; a = 19???;
/* remember the saved value */
a = 9
Or, as you invoked undefined behaviour, it could simply replace your statement with any of the following
- a = 42;
- /* */
- fprintf(stderr, "BANG!");
- system("format C:");
- for (p=0; p<MEMORY_SIZE; p++) *p = 0;
- etc ...