看以下例子便可理解:
public class Test_i {
public static void main(String[] args) {
int i = 0;
System.out.println("运算前i为"+i+"执行"+ i++ +",此时i为"+i);
System.out.println("运算前i为"+i+"执行"+ ++i +",此时i为"+i);
System.out.println("运算前i为"+i+"执行"+ i-- +",此时i为"+i);
System.out.println("运算前i为"+i+"执行"+ --i +",此时i为"+i);
System.out.println("i++执行完表达式值不变 i值+1");
}
}
再来道面试题:
static {
int x =5;
}
static int x,y;
public static void main(String[] args) {
x--;
myMethod();
System.out.println(x+ y++ + x);
}
public static void myMethod(){
y = x++ + ++x;
}
答案:2.
答对了吗?
时间: 2024-10-08 05:58:14