“循环:”在 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
"loop:" in Java code. What is this, and why does it compile?
提问by Amy B
This code just made me stare at my screen for a few minutes:
这段代码让我盯着屏幕看了几分钟:
loop:
for (;;) {
// ...
}
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 keyword
it 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;
}
}
}
文档。
回答by codaddict
回答by PaulJWilliams
回答by vstoyanov
It is a label, and labels in Java can be used with the break
and continue
key words for additional control over loops.
它是一个标签,Java 中的标签可以与break
和continue
关键字一起使用,以对循环进行额外控制。
Here it is explained in a rather good way:
这里以一种相当好的方式解释了它:
回答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.
这允许断开外环。
回答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>10
the 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 break
and continue
keywords inside nested loops:
这不是关键字,而是标签。它旨在与嵌套循环内的break
andcontinue
关键字一起使用:
outer:
for(;;){
inner:
for(;;){
if(){
break inner; // ends inner loop
} else {
break outer; // ends outer loop
}
}
}
回答by SoulWanderer
回答by greuze
It is not a keyword, but a label. If inside the for
loop 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, url
and http
, a comment www.myserver.com/myfile.mp3
and 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).
解决方案:两个标签,url
和http
,一个注释www.myserver.com/myfile.mp3
和一个方法调用,参数url
与标签同名 ( )。是的,这会编译(如果您在其他地方定义方法调用和局部变量)。