自增运算

2024-07-05 13:24:20 浏览数 (1)

请写出如下代码的运行结果:

主要考察自增运算

代码语言:javascript复制
public class Test1 {
    static int x, y;
    static {
        x = 5;
    }
    public static void main(String[] args) {
        x --;
        someMethod();
        System.out.println(x   y     x);
    }
    private static void someMethod() {
        y = x       x;
    }
}

逐条分析

代码语言:javascript复制
public class Test1 {
    static int x, y;
    static {
        x = 5;
    }
    public static void main(String[] args) {
        x --; // x = 4
        someMethod();
        System.out.println(x   y     x); // 6   10   (6 1) = 23
    }
    private static void someMethod() {
        y = x       x; 
       // x = (4   1)   1 = 6
       // y = (4   (4   1))   1 = 10
    }
}

结果为23

0 人点赞