1. 程式人生 > >關於 ++x 和 x++ 比較難的一個例子

關於 ++x 和 x++ 比較難的一個例子

public class testMain {
    static int x;
    static int y;
    public static void main(String[] args) {
        x = 0; y = 0;
        System.out.println("x:"+x+" y:"+y);
        x++;
        System.out.println("x:"+x+" y:"+y);
        myMethod();
        System.out.println("x:"+x+" y:"+y);
        System.out.println(x 
+ y + ++x); } public static void myMethod(){ y = x++ + ++x; } }

Result:

x:0 y:0
x:1 y:0
x:3 y:4
11