When I run this class, the for loop seems to end the early
class Test { public static void main(String[] args) { int result = 0; int end = Integer.MAX_VALUE; int i; for (i = 1; i <= end; i += 2) { System.out.println(i); } System.out.println("End:" + i); } }
Exit:
1 3 5 ... 31173 31175 End:31177
Why does it end? Interestingly, if I deleted System.out.println(i) in a for loop, the output would be End:-2147483647 . Obviously, the value in i is wrapped round .
The used version of Java is
Java(TM) SE Runtime Environment (build 1.6.0_16-b01) Java HotSpot(TM) 64-Bit Server VM (build 14.2-b01, mixed mode)
java max int for-loop
Ben
source share