The compiler can easily and unequivocally prove that the first expression always results in an infinite loop, but it's not as easy for the second. In your toy example it's simple, but what if:
- the variable's contents were read from a file?
- the variable wasn't local and could be modified by another thread?
- the variable relied on some user input?
The compiler is clearly not checking for your simpler case because it's forgoing that road altogether. Why? Because it's much harder forbidden by the spec. See section 14.21:
- http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21-300-M
(By the way, my compiler does complain when the variable is declared final.)
Videos
What problems can arise from infinite loops in a Java program?
How can I terminate or break out of an infinite loop in Java?
How can I handle intentional infinite loops to prevent unwanted consequences?
The compiler can easily and unequivocally prove that the first expression always results in an infinite loop, but it's not as easy for the second. In your toy example it's simple, but what if:
- the variable's contents were read from a file?
- the variable wasn't local and could be modified by another thread?
- the variable relied on some user input?
The compiler is clearly not checking for your simpler case because it's forgoing that road altogether. Why? Because it's much harder forbidden by the spec. See section 14.21:
- http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21-300-M
(By the way, my compiler does complain when the variable is declared final.)
According the specifications, the following is said about while statements.
A while statement can complete normally iff at least one of the following is true:
- The while statement is reachable and the condition expression is not a constant expression with value true.
- There is a reachable break statement that exits the while statement.\
So, the compiler will only say that the code following a while statement is unreachable if the while condition is a constant with a true value, or there is a break statement within the while. In the second case, since the value of b is not a constant, it doesn't consider the code following it to be unreachable. There's a whole lot more information behind that link to give you more details on just what is and what isn't considered unreachable.
There is no difference in bytecode between while(true) and for(;;) but I prefer while(true) since it is less confusing (especially for someone new to Java).
You can check it with this code example
void test1(){
for (;;){
System.out.println("hello");
}
}
void test2(){
while(true){
System.out.println("world");
}
}
When you use command javap -c ClassWithThoseMethods you will get
void test1();
Code:
0: getstatic #15 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #21 // String hello
5: invokevirtual #23 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: goto 0
void test2();
Code:
0: getstatic #15 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #31 // String world
5: invokevirtual #23 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: goto 0
which shows same structure (except "hello" vs "world" strings) .
I prefer while(true), because I use while loops less often than for loops. For loops have better uses and while(true) is much cleaner and easy to read than for(;;)