Why is it that the code below behaves differently in Java 1.6 and 1.7 -
the code below
public class test16jit { public static void main(string[] s) { int max = integer.max_value; int = 0; long li = 0; while (i >= 0) { i++; li++; if (i > max) { system.out.println("i : " + i); system.out.println("max : " + max); system.out.println("woo!! went wrong"); } } system.out.println("value of i: " + i); system.out.println("total # of iterations: " + li); } }
outputs below in java 1.7x
value of i: -2147483648 total # of iterations: 2147483648
outputs below in java 1.6x
i : 2147483636 max : 2147483647 woo!! went wrong value of i: -2147483648 total # of iterations: 2147483648
is there reason behavior?
also if change
int max = integer.max_value; -> final int max = integer.max_value;
it behaves same in 1.6x , 1.7x
it seems 1 of many examples of family of errors related 1 caused jit compilation (i picked 1 due similarity of code, feel free explore others - quite interesting!):
http://bugs.java.com/view_bug.do?bug_id=6196102
evaluation
problem canonicalization of loop exit test in preparation loop transformation.
do while (++i <= limit)
becomes
do while (++i < limit+1)
this isn't correct when limit maxint.
some issues fixed in 1.7, explain results.
Comments
Post a Comment