Faisons inspectent avec mon outil préféré: javap ...
Le bytecode émis par Suns javac pour
int a = 0;
int b = 0;
b = a++ + ++a + ++a;
se présente comme suit:
a b Stack:
0: iconst_0 // push 0 0 0 0
1: istore_1 // store in a 0 0 empty
2: iconst_0 // push 0 0 0 0
3: istore_2 // store in b 0 0 empty
4: iload_1 // push value of a 0 0 0
5: iinc 1, 1 // inc a with 1 1 0 0
8: iinc 1, 1 // inc a with 1 again 2 0 0
11: iload_1 // push value of a again 2 0 0,2
12: iadd // add top two elements 2 0 2
13: iinc 1, 1 // inc a with 1 3 0 2
16: iload_1 // push a again 3 0 2,3
17: iadd // add top two elements 3 0 5
18: istore_2 // store in b 3 5 empty
Il sera ecaluated comme b = 0 + 2 + 3 et à la fin b sera 5 et a sera 3. – jaxb
Très probablement un doublon. – ripper234
duplication possible de [expliquer travail de post et pré opérateur d'incrémentation en Java] (http://stackoverflow.com/questions/2371118/explain-working-of-post-and-pre-increment-operator-in-java) –