“循环:”在 Java 代码中。这是什么,为什么要编译?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3821827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 05:30:55  来源:igfitidea点击:

"loop:" in Java code. What is this, and why does it compile?

javalabeled-statements

提问by Amy B

This code just made me stare at my screen for a few minutes:

这段代码让我盯着屏幕看了几分钟:

loop:
for (;;) {
    // ...
}

(line 137 here)

此处为第 137 行

I have never seen this before, and I had no idea Java has a "loop" keyword (NetBeans doesn't even color it like a keyword), and it does compile fine with JDK 6.

我以前从未见过这个,我不知道 Java 有一个“循环”关键字(NetBeans 甚至不会像关键字那样给它上色),而且它在 JDK 6 中编译得很好。

What is the explanation?

解释是什么?

采纳答案by Jigar Joshi

It is not a keywordit is a label.

它不是一个keyword它是一个label

Usage:

用法:

    label1:
    for (; ; ) {
        label2:
        for (; ; ) {
            if (condition1) {
                // break outer loop
                break label1;
            }
            if (condition2) {
                // break inner loop
                break label2;
            }
            if (condition3) {
                // break inner loop
                break;
            }
        }
    }

Documentation.

文档

回答by codaddict

It's not a keyword; it's a label.

它不是关键字;这是一个标签

It allows you to go a labeled breakand labeled continue.

它允许你去一个标记break和标记continue

回答by PaulJWilliams

It's a break point label, to allow you to break out of a specified loop, rather than simply the innermost one you happen to be in.

它是一个断点标签,允许您跳出指定的循环,而不仅仅是您碰巧所在的最里面的循环。

It's used on line 148.

它在第 148 行使用。

回答by vstoyanov

It is a label, and labels in Java can be used with the breakand continuekey words for additional control over loops.

它是一个标签,Java 中的标签可以与breakcontinue关键字一起使用,以对循环进行额外控制。

Here it is explained in a rather good way:

这里以一种相当好的方式解释了它:

Thinking in Java, break and continue

用 Java 思考,中断并继续

回答by Rob Di Marco

As other posters have said, it is a label, not a keyword. Using labels allows you to do things like:

正如其他海报所说,它是一个标签,而不是一个关键词。使用标签可以执行以下操作:

outer: for(;;) {
   inner: for(;;) {
     break outer;
   }
}

This allows for breaking of the outer loop.

这允许断开外环。

Link to documentation.

链接到文档

回答by Alberto Zaccagni

It's a label, though look at the following example:

这是一个标签,但请看以下示例:

int a = 0;
int b = 0
while (a<10){
    firstLoop:
    a++;
    while(true){
        b++
        if(b>10){
            break firstLoop;
        }
    }
 }

When b>10the execution flow goes to the outer loop.

b>10执行流程转到外循环时。

回答by Michael Borgwardt

That's not a keyword, it's a label. It's meant to be used with the breakand continuekeywords inside nested loops:

这不是关键字,而是标签。它旨在与嵌套循环内的breakandcontinue关键字一起使用:

outer:
for(;;){
    inner:
    for(;;){
        if(){
            break inner; // ends inner loop
        } else {
            break outer; // ends outer loop
        }
    }
}

回答by SoulWanderer

You could write almost anything, as it is a label... You have an example here

你几乎可以写任何东西,因为它是一个标签......你有一个例子here

回答by greuze

It is not a keyword, but a label. If inside the forloop you write break loop;, and you exit that loop.

它不是关键字,而是标签。如果在for循环中你写了break loop;,你就退出循环。

回答by Sean Patrick Floyd

The question is answered, but as a side note:

这个问题得到了回答,但作为旁注:

I have heard of interview questions a la "Why is this Java code valid?" (stripped the simpler example; here's the meaner one, thx Tim Büthe):

我听说过“为什么这个 Java 代码有效?”这样的面试问题。(去掉了更简单的例子;这是更简单的例子,感谢 Tim Büthe):

url: http://www.myserver.com/myfile.mp3
downLoad(url);

Would you all know what this code is (apart from awful)?

你们都知道这个代码是什么(除了可怕的)?

Solution: two labels, urland http, a comment www.myserver.com/myfile.mp3and a method call with a parameter that has the same name (url) as the label. Yup, this compiles (if you define the method call and the local variable elsewhere).

解决方案:两个标签,urlhttp,一个注释www.myserver.com/myfile.mp3和一个方法调用,参数url与标签同名 ( )。是的,这会编译(如果您在其他地方定义方法调用和局部变量)。