下面的代码
public class Test16Jit {
public static void main(String[] s) {
int max = Integer.MAX_VALUE;
int i = 0;
long li = 0;
while (i >= 0) {
i++;
li++;
if (i > max) {
System.out.println("i is : " + i);
System.out.println("max is : " + max);
System.out.println("Woo!! something really went wrong");
}
}
System.out.println("Value of i: " + i);
System.out.println("Total # of iterations: " + li);
}
}
java 1.7x中的输出如下
Value of i: -2147483648
Total # of iterations: 2147483648
Java 1.6x中的输出如下
i is : 2147483636
max is : 2147483647
Woo!! something really went wrong
Value of i: -2147483648
Total # of iterations: 2147483648
这种行为有原因吗?
如果我改变了
int max = Integer.MAX_VALUE; -> final int max = Integer.MAX_VALUE;
它在1.6x和1.7x中表现完全相同