i tested your code with jdk 1.7.0_21 and it returned the same result, 942210, with or without the LOG.debug statement.
int i = 0;
for( ; !rules(i) && i < Integer.MAX_VALUE ; i++ ){
//LOG.debug( "test " + i );
}
System.out.println( "i is " + i );
and i also printed out the byte code of both version (empty loop)
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
loop with debug printout
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
as you can see, the loop structure is identical. 20 - 39 is just constructing the string and call LOG.debug.
so, it could be a jdk issue. try 1.7, it should work.
int
tolong
. Maybe there is a silent value overflow. – spasint
tolong
and keeping an empty loop, i get the right result. Funny though, cause 16698 is very far from the maximal value of int in Java (2147483647, cf. docs.oracle.com/javase/tutorial/java/nutsandbolts/…) – Dirty Henry