Java是否识别无限循环? [英] Does Java recognize infinite loops?

查看:150
本文介绍了Java是否识别无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码示例:

public class WeirdStuff {

    public static int doSomething() {
        while(true);
    }

    public static void main(String[] args) {
        doSomething();
    }
}

这是一个有效的Java程序,虽然方法是doSomething ()应该返回一个int,但永远不会。如果你运行它,它将以无限循环结束。如果你将while循环的参数放在一个单独的变量中(例如boolean bool = true),编译器会告诉你在这个方法中返回一个int。

This is a valid Java program, although the method doSomething() should return an int but never does. If you run it, it will end in an infinite loop. If you put the argument of the while loop in a separate variable (e.g. boolean bool = true) the compiler will tell you to return an int in this method.

所以我的问题是:这是Java规范中的某个地方,是否存在这种行为可能有用的情况?

So my question is: is this somewhere in the Java specification and are there situation where this behavior might be useful?

推荐答案

我只是引用 Java语言规范,因为它相当清楚:

I'll just quote the Java Language Specification, as it's rather clear on this:


本节专门用于对可达一词的精确解释。我们的想法是,从构造函数,方法,实例初始化程序或包含语句本身的静态初始化程序开始,必须有一些可能的执行路径。分析考虑了陈述的结构。除了特殊处理while,do和for条件表达式具有常量值true 的语句之外,在流分析中不考虑表达式的值。

This section is devoted to a precise explanation of the word "reachable." The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.

...


while语句至少可以正常完成iff以下之一为真:

A while statement can complete normally iff at least one of the following is true:


  • 可以访问while语句,条件表达式不是值为true的常量表达式。

  • 有一个可到达的break语句退出while语句。

...


如果在S之前的语句,则非空块中的非交换块中的每个其他语句S都是可到达的可以正常完成。

Every other statement S in a nonempty block that is not a switch block is reachable iff the statement preceding S can complete normally.

然后将上述定义应用于这个

And then apply the above definitions to this:


如果声明某个方法具有返回类型,则其主体中的每个return语句(第14.17节)都必须具有Expression。如果方法的主体可以正常完成,则会发生编译时错误(第14.1节)。

If a method is declared to have a return type, then every return statement (§14.17) in its body must have an Expression. A compile-time error occurs if the body of the method can complete normally (§14.1).

换句话说,具有返回类型的方法必须仅使用提供值返回的return语句返回;它不允许从它的主体末端掉落。

In other words, a method with a return type must return only by using a return statement that provides a value return; it is not allowed to "drop off the end of its body."

注意,方法可能有一个声明的返回类型但是不包含return语句。这是一个例子:

Note that it is possible for a method to have a declared return type and yet contain no return statements. Here is one example:

class DizzyDean {
  int pitch() { throw new RuntimeException("90 mph?!"); }
}


这篇关于Java是否识别无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆