在Java下面爲什麼是它下面的代碼中表現不同的Java 1.6和1.7
下面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);
}
}
輸出碼1.7倍
Value of i: -2147483648
Total # of iterations: 2147483648
下面
在產出的Java 1.6倍
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.6倍完全相同和1.7倍
你是否從簡單的代碼'int max = Integer.MAX_VALUE; int i = 2147483636; System.out.println(i> max);'? –
@AndyTurner我還沒有測試,但我猜這應該是一個JIT錯誤。 – shmosel
打印false @AndyTurner –