关于 ++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

猜你喜欢

转载自www.cnblogs.com/wmxl/p/9811800.html