Java 能识别无限循环吗? [英] Does Java recognize infinite loops?

查看:28
本文介绍了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的特殊处理,以及条件表达式为常量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 语句可以正常完成,如果以下至少一项为真:

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

  • while 语句是可达的,并且条件表达式不是值为 true 的常量表达式.
  • 有一个可到达的 break 语句退出 while 语句.

...

如果 S 前面的语句可以正常完成,则非 switch 块的非空块中的所有其他语句 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:

如果方法声明为具有返回类型,则其主体中的每个返回语句(第 14.17 节)都必须具有表达式.如果方法的主体可以正常完成(第 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."

请注意,方法可能具有声明的返回类型,但不包含返回语句.下面是一个例子:

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天全站免登陆