下面的代码
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.6倍和1.7倍时其行为完全相同
似乎是由JIT编译引起的与此错误相关的一系列错误的许多示例之一(由于代码的相似性,我选择了此错误,请随时探索其他错误-它们非常有趣!):
http://bugs.java.com/view_bug.do?bug_id=6196102
评价
问题在于循环出口测试的规范化以准备循环转换。
do while (++i <= limit)
变成
do while (++i < limit+1)
当limit为maxInt时,这是不正确的。
1.7中修复了一些问题,可以解释您的结果。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句