看下面样例便可理解:
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.
答对了吗?