Java中的无限循环 [英] Infinite loops in Java
问题描述
在Java中查看以下无限而
循环。它会导致它下面的语句出现编译时错误。
Look at the following infinite while
loop in Java. It causes a compile-time error for the statement below it.
while(true) {
System.out.println("inside while");
}
System.out.println("while terminated"); //Unreachable statement - compiler-error.
以下相同的无限而
循环,但工作正常,并没有发出任何错误,我只是用一个布尔变量替换条件。
The following same infinite while
loop, however works fine and doesn't issue any errors in which I just replaced the condition with a boolean variable.
boolean b=true;
while(b) {
System.out.println("inside while");
}
System.out.println("while terminated"); //No error here.
在第二种情况下,声明在循环显然无法访问,因为布尔变量 b
为true,编译器根本不会抱怨。为什么?
In the second case also, the statement after the loop is obviously unreachable because the boolean variable b
is true still the compiler doesn't complain at all. Why?
编辑:以下版本的而
显然陷入了无限循环,但是对于它下面的语句没有发出编译器错误,即使循环中的 if
条件总是 false
因此,循环永远不会返回,并且可以由编译器在编译时自己确定。
Edit : The following version of while
gets stuck into an infinite loop as obvious but issues no compiler errors for the statement below it even though the if
condition within the loop is always false
and consequently, the loop can never return and can be determined by the compiler at the compile-time itself.
while(true) {
if(false) {
break;
}
System.out.println("inside while");
}
System.out.println("while terminated"); //No error here.
while(true) {
if(false) { //if true then also
return; //Replacing return with break fixes the following error.
}
System.out.println("inside while");
}
System.out.println("while terminated"); //Compiler-error - unreachable statement.
while(true) {
if(true) {
System.out.println("inside if");
return;
}
System.out.println("inside while"); //No error here.
}
System.out.println("while terminated"); //Compiler-error - unreachable statement.
编辑相同如果
和而
。
if(false) {
System.out.println("inside if"); //No error here.
}
while(false) {
System.out.println("inside while");
// Compiler's complain - unreachable statement.
}
while(true) {
if(true) {
System.out.println("inside if");
break;
}
System.out.println("inside while"); //No error here.
}
以下版本的而
也会陷入无限循环。
while(true) {
try {
System.out.println("inside while");
return; //Replacing return with break makes no difference here.
} finally {
continue;
}
}
这是因为最终
即使 return
语句在尝试
块本身遇到它之前总是执行块。
This is because the finally
block is always executed even though the return
statement encounters before it within the try
block itself.
推荐答案
编译器可以轻松,明确地证明第一个表达式总是导致无限循环,但这对第二个来说并不那么容易。在你的玩具示例中,这很简单,但是如果:
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:
- 变量的内容是从文件中读取的?
- 变量不是本地的,可以被另一个线程修改?
- 该变量依赖于某些用户输入?
编译器显然没有检查你的简单案例,因为它完全放弃了这条道路。为什么?因为规范禁止使用更难。见第14.21节:
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
(顺便说一句,我的编译器 抱怨变量声明为 final
。)
(By the way, my compiler does complain when the variable is declared final
.)
这篇关于Java中的无限循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!