简体   繁体   English

循环布尔评估

[英]While loop boolean evaluation

Example 1: 例1:

public class ExampleWhile {

    public static void main(String args[]) {
        int a = 10, b = 20;
        while (a < b) 
        {
            System.out.println("hello");
        }
        System.out.println("hi");
    }
}

Example 2: 例2:

public class ExampleWhile2 {

        public static void main(String args[]) {
            while (true) 
            {
                System.out.println("hello");
            }
            System.out.println("hi"); // Compile time error saying unreachable statement
        }
    }

Why there is a compile time error in example 2 when example 1 runs without error? 为什么示例1在没有错误的情况下运行时,示例2中存在编译时错误?

Because the compiler is "clever" enough to know that while(true) is an infinite loop, hence System.out.println("hi"); 因为编译器“聪明”足以知道while(true)是一个无限循环,因此System.out.println("hi"); would never be executed. 永远不会被执行。

It is not clever enough to infer the same with variables, even when scoped to the method. 即使在确定方法范围时,用变量推断相同也不够聪明。

With compile-time constants ( static final int makes the integer a compile-time constant . Compile time constants will be available as part of the byte-code itself ), it's another story: 使用编译时常量static final int使整数成为编译时常量编译时常量作为字节代码本身的一部分提供 ),这是另一个故事:

static final int A = 0;
static final int B = 1;
public static void main(String[] args) {
    while (B > A) {

    }
    // won't compile!
    System.out.println("Foo");
}

The compiler does not know about the values of a and b, so it doesn't see it as an infinite loop with an unreachable statement. 编译器不知道a和b的值,因此它不会将其视为具有无法访问语句的无限循环。

With while(true), on the other hand, the compiler knows there 'll be trouble. 有了while(true),另一方面,编译器知道会有麻烦。

The Java Language Specification gives specific rules, in 14.21. Java语言规范给出了具体的规则,见14.21。 Unreachable Statements , for the compile time error. 无法访问的语句 ,编译时错误。

The rule that applies to the sample code is: 适用于示例代码的规则是:

A while statement can complete normally iff at least one of the following is true: 如果至少满足下列条件之一,则while语句可以正常完成:

 The while statement is reachable and the condition expression is not a constant expression (§15.28) with value true. There is a reachable break statement that exits the while statement. 

The first of the two conditions is true for Example 1, but false for Example 2. The second condition is false for both examples. 对于示例1,两个条件中的第一个为真,对于示例2,则为假。对于两个示例,第二个条件为假。 As far as the language specification is concerned, there is a possibility that the first loop might complete normally, allowing control to reach the following statement. 就语言规范而言,第一个循环可能正常完成,允许控制达到以下语句。 There is no such possibility for the second example. 第二个例子没有这种可能性。

The rules achieve two benefits. 规则有两个好处。 There is a single set of rules for whether a sequence of characters constitutes a compilable Java program, regardless of the choice of compiler. 无论编译器的选择如何,对于一系列字符是否构成可编译的Java程序,都有一套规则。 The rules can be implemented very simply. 规则可以非常简单地实施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM