java Java标签?外、中、内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27696712/
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
Java label? Outer, middle, inner
提问by OPK
Please don't worry about the loop but my question is about those keywords: outer
, middle
, and inner
.They are not declared as instance variable, why the IDE let the code compile? I did some search on google, is this java label? some kind of keywords in Java? Thanks guys.
请做一下环不用担心,但我的问题是关于这些关键字:outer
,middle
,和inner
。他们不声明为实例变量,为什么IDE让代码编译?我在谷歌上搜索了一下,这是java标签吗?Java中的某种关键字?多谢你们。
public class LoopTest{
public static void main(String[] args){
int counter = 0;
outer:
for(int i = 0; i < 3; i++){
middle:
for(int j = 0; j < 3; j++){
inner:
for(int k = 0; k < 3; k++){{
}
if(k - j > 0){
break middle;
}
counter++;
}
}
}
System.out.println(counter);
}
}
}
回答by wassgren
Java supports labels. This is described in this article from Oracle.
Java 支持标签。这在Oracle 的这篇文章中有所描述。
So, basically you can have loops with labels and you can use keyword continue
, break
and so on to control the flow of the loop.
所以,基本上你可以有标签的循环,你可以使用关键字continue
,break
等来控制循环的流程。
The following sample illustrates how to use the loop with the break
keyword. When break
is invoked it terminates the labeled statement i.e. the statement following someLabel
以下示例说明了如何使用带有break
关键字的循环。当break
被调用时,它终止标记的语句,即下面的语句someLabel
someLabel:
for (i = 0; i < 100; i++) {
for (j = 0; j < 100; j++) {
if (i % 20 == 0) {
break someLabel;
}
}
}
The continue
keyword handles labels the same way. When you invoke e.g. continue someLabel;
the outer loop will be continued.
该continue
关键字手柄标签的方法。当您调用例如continue someLabel;
外循环将继续。
As per this SO-questionyou can also do constructs like this:
由于按照该SO-问题,你也可以做这样的结构:
BlockSegment:
if (conditionIsTrue) {
doSomeProcessing ();
if (resultOfProcessingIsFalse()) break BlockSegment;
otherwiseDoSomeMoreProcessing();
// These lines get skipped if the break statement
// above gets executed
}
// This is where you resume execution after the break
anotherStatement();
Personally, I would never recommend using labels. Instead I find that the code gets easier to follow if you instead rearrange your code so that labels are not needed (by e.g. break out complex code to smaller functions).
就个人而言,我永远不会推荐使用标签。相反,我发现如果您重新排列代码以便不需要标签(例如,通过将复杂代码分解为更小的函数),代码会变得更容易理解。
回答by corsiKa
Early in Java, there was a goto
operator. One day, James Gosling decided to remove it. But, it turned out there was still use in allowing you to use goto
inside loops. So how to do this? Well, with named loops (also known as labeled loops) you could have all the good stuff of breaking out of loops without the downsides of goto
ridden spaghetti code.
在 Java 早期,有一个goto
运算符。有一天,James Gosling 决定将其移除。但是,事实证明仍然可以使用允许您使用goto
内部循环。那么如何做到这一点呢?好吧,使用命名循环(也称为标记循环),您可以拥有打破循环的所有好东西,而不会出现冗长的goto
意大利面条式代码的缺点。
So named loops became a thing, and break
and continue
were allowed to break or continue a loop other than their immediate parent.
所以命名为循环变成了东西,break
并continue
允许中断或继续比它们的直接父以外的循环。
回答by Lolo
Yes, these are labeled statements, as described in the Java Langage Specification, Java SE 8 Edition, in section 14.7:
是的,这些是标记语句,如 Java 语言规范,Java SE 8 版,第 14.7 节中所述:
LabeledStatement:
Identifier : Statement
LabeledStatementNoShortIf:
Identifier : StatementNoShortIf
The Identifier is declared to be the label of the immediately contained Statement.
Unlike C and C++, the Java programming language has no goto statement; identifier statement labels are used with break or continue statements (§14.15, §14.16) appearing anywhere within the labeled statement.
The scope of a label of a labeled statement is the immediately contained Statement.
It is a compile-time error if the name of a label of a labeled statement is used within the scope of the label as a label of another labeled statement.
There is no restriction against using the same identifier as a label and as the name of a package, class, interface, method, field, parameter, or local variable. Use of an identifier to label a statement does not obscure (§6.4.2) a package, class, interface, method, field, parameter, or local variable with the same name. Use of an identifier as a class, interface, method, field, local variable or as the parameter of an exception handler (§14.20) does not obscure a statement label with the same name.
A labeled statement is executed by executing the immediately contained Statement.
If the statement is labeled by an Identifier and the contained Statement completes abruptly because of a break with the same Identifier, then the labeled statement completes normally. In all other cases of abrupt completion of the Statement, the labeled statement completes abruptly for the same reason.
标识符被声明为立即包含的语句的标签。
与 C 和 C++ 不同,Java 编程语言没有 goto 语句;标识符语句标签与出现在标记语句内任何位置的 break 或 continue 语句(第 14.15 节、第 14.16 节)一起使用。
带标签语句的标签范围是直接包含的语句。
如果在标签范围内将一个带标签语句的标签名称用作另一个带标签语句的标签,则会导致编译时错误。
没有限制使用相同的标识符作为标签和包、类、接口、方法、字段、参数或局部变量的名称。使用标识符来标记语句不会混淆(第 6.4.2 节)具有相同名称的包、类、接口、方法、字段、参数或局部变量。将标识符用作类、接口、方法、字段、局部变量或作为异常处理程序的参数(第 14.20 节)不会混淆具有相同名称的语句标签。
通过执行直接包含的 Statement 来执行带标签的语句。
如果语句由标识符标记并且包含的语句由于具有相同标识符的中断而突然完成,则标记语句正常完成。在语句突然完成的所有其他情况下,标记语句出于相同原因突然完成。