简体   繁体   English

Java标签? 外,中,内

[英]Java label? Outer, middle, inner

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? 请不要担心循环,但我的问题是关于那些关键字: outermiddleinner 。它们不被声明为实例变量,为什么IDE让代码编译? I did some search on google, is this java label? 我在google上做了一些搜索,是这个java标签吗? some kind of keywords in Java? Java中的某种关键字? Thanks guys. 多谢你们。

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);
}

} }

Java supports labels. Java支持标签。 This is described in this article from Oracle . 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. 所以,基本上你可以有带标签的循环,你可以使用关键字continuebreak等来控制循环的流程。

The following sample illustrates how to use the loop with the break keyword. 以下示例说明了如何将循环与break关键字一起使用。 When break is invoked it terminates the labeled statement ie the statement following someLabel 当调用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. continue关键字以相同的方式处理标签。 When you invoke eg continue someLabel; 当你调用例如continue someLabel; the outer loop will be continued. 外循环将继续。

As per this SO-question you 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 eg break out complex code to smaller functions). 相反,如果您重新排列代码以便不需要标签(例如,将复杂代码分解为更小的函数),我发现代码更容易理解。

Early in Java, there was a goto operator. 在Java早期,有一个goto运算符。 One day, James Gosling decided to remove it . 有一天, 詹姆斯戈斯林决定将其删除 But, it turned out there was still use in allowing you to use goto inside loops. 但是,事实证明,仍允许你在循环中使用goto 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. 好吧,使用命名循环(也称为标记循环),您可以拥有突破循环的所有好东西,而不会受到goto的意大利面条代码的缺点。

So named loops became a thing, and break and continue were allowed to break or continue a loop other than their immediate parent. 所以命名循环变成了一个东西,并且允许breakcontinue断开或继续循环而不是它们的直接父级。

Yes, these are labeled statements , as described in the Java Langage Specification, Java SE 8 Edition, in section 14.7 : 是的,这些是带标签的语句 ,如Java Langage规范,Java SE 8 Edition, 第14.7节中所述

LabeledStatement:
  Identifier : Statement
LabeledStatementNoShortIf:
  Identifier : StatementNoShortIf

The Identifier is declared to be the label of the immediately contained Statement. 标识符被声明为直接包含的Statement的标签。

Unlike C and C++, the Java programming language has no goto statement; 与C和C ++不同,Java编程语言没有goto语句; identifier statement labels are used with break or continue statements (§14.15, §14.16) appearing anywhere within the labeled statement. 标识符语句标签与出现在标记语句中任何位置的break或continue语句(第14.15节,第14.16节)一起使用。

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. 使用标识符标记语句不会模糊(第6.4.2节)具有相同名称的包,类,接口,方法,字段,参数或局部变量。 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. 使用标识符作为类,接口,方法,字段,局部变量或作为异常处理程序的参数(第14.20节)不会模糊具有相同名称的语句标签。

A labeled statement is executed by executing the immediately contained Statement. 标记语句通过执行立即包含的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. 在突然完成声明的所有其他情况下,标签声明因同样的原因突然完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM