简体   繁体   中英

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? I did some search on google, is this java label? some kind of keywords in 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. This is described in this article from 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.

The following sample illustrates how to use the loop with the break keyword. When break is invoked it terminates the labeled statement ie the statement following 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 eg continue someLabel; the outer loop will be continued.

As per this SO-question you can also do constructs like this:

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. 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.

So named loops became a thing, and break and continue were allowed to break or continue a loop other than their immediate parent.

Yes, these are labeled statements , as described in the Java Langage Specification, Java SE 8 Edition, in section 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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