我用 jdk 1.7.0_21 测试了你的代码,它返回了相同的结果,942210,有或没有 LOG.debug 语句。
int i = 0;
for( ; !rules(i) && i < Integer.MAX_VALUE ; i++ ){
//LOG.debug( "test " + i );
}
System.out.println( "i is " + i );
我还打印了两个版本的字节码(空循环)
0: aload_0
1: invokespecial #3 // Method java/lang/Object."<init>":()V
4: iconst_0
5: istore_1
6: aload_0
7: iload_1
8: invokevirtual #4 // Method rules:(I)Z
11: ifne 26
14: iload_1
15: ldc #5 // int 2147483647
17: if_icmpge 26
20: iinc 1, 1
23: goto 6
26: getstatic #6 // Field java/lang/System.out:Ljava/io/PrintStream;
29: new #7 // class java/lang/StringBuilder
32: dup
33: invokespecial #8 // Method java/lang/StringBuilder."<init>":()V
36: ldc #9 // String i is
38: invokevirtual #10 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
41: iload_1
42: invokevirtual #11 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
45: invokevirtual #12 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
48: invokevirtual #13 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
51: return
带调试打印输出的循环
0: aload_0
1: invokespecial #3 // Method java/lang/Object."<init>":()V
4: iconst_0
5: istore_1
6: aload_0
7: iload_1
8: invokevirtual #4 // Method rules:(I)Z
11: ifne 48
14: iload_1
15: ldc #5 // int 2147483647
17: if_icmpge 48
20: new #6 // class java/lang/StringBuilder
23: dup
24: invokespecial #7 // Method java/lang/StringBuilder."<init>":()V
27: ldc #8 // String test
29: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
32: iload_1
33: invokevirtual #10 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
36: invokevirtual #11 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
39: invokestatic #12 // Method LOG.debug:(Ljava/lang/String;)V
42: iinc 1, 1
45: goto 6
48: getstatic #13 // Field java/lang/System.out:Ljava/io/PrintStream;
51: new #6 // class java/lang/StringBuilder
54: dup
55: invokespecial #7 // Method java/lang/StringBuilder."<init>":()V
58: ldc #14 // String i is
60: invokevirtual #9 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
63: iload_1
64: invokevirtual #10 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
67: invokevirtual #11 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
70: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
73: return
如您所见,循环结构是相同的。20 - 39 只是构造字符串并调用 LOG.debug。
所以,这可能是一个jdk问题。尝试1.7,它应该可以工作。