how is this possible as post increment operator should increase x to 66?
When i did the same for y= ++x + ++x + x++; it gave a value 65 for y and 23 for x.
So let me know how is java compilers solving these expression.
解决方案
Let Java show you. javap -c MyClass shows you bytecode:
public static void main(java.lang.String[]);
Code:
0: bipush 20
2: istore_1
3: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
6: iinc 1, 1
9: iload_1
10: iinc 1, 1
13: iload_1
14: iadd
15: iload_1
16: iinc 1, 1
19: iadd
20: dup
21: istore_1
22: invokevirtual #3 // Method java/io/PrintStream.println:(I)V
25: return
And the result is completely logical if you think about it: you have two preincrements and one postincrement. So, your code is in effect:
y = 0
x++ // 21
y += x
x++ // 22
y += x
y += x // (still 22!)
x++ // 23
x = y // (21 + 22 + 22 at this point)