Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Labeled Statement block in Java?

Tags:

java

syntax

I was browsing through some of the base Java objects when I found a section of code surrounded by a scan: {} block. The following code is from the toLowerCase() method inside the String class.

scan: {
            for (firstUpper = 0 ; firstUpper < len; ) {
                char c = value[firstUpper];
                if ((c >= Character.MIN_HIGH_SURROGATE)
                        && (c <= Character.MAX_HIGH_SURROGATE)) {
                    int supplChar = codePointAt(firstUpper);
                    if (supplChar != Character.toLowerCase(supplChar)) {
                        break scan;
                    }
                    firstUpper += Character.charCount(supplChar);
                } else {
                    if (c != Character.toLowerCase(c)) {
                        break scan;
                    }
                    firstUpper++;
                }
            }
            return this;
        }

Could someone please explain what the scan:{} block is used for and where this syntax comes from? I've yet to see a colon after a word like this in Java unless used in a ternary operator.

Thanks!

Edit: Updated title to correctly match answered question.

like image 972
Falkenfighter Avatar asked Jan 03 '13 21:01

Falkenfighter


People also ask

What is Labelled statement?

labeled-statement: identifier : statement. A statement label is meaningful only to a goto statement; in any other context, a labeled statement is executed without regard to the label. A jump-statement must reside in the same function and can appear before only one statement in the same function.

What are labels in Java?

A Label object is a component for placing text in a container. A label displays a single line of read-only text. The text can be changed by the application, but a user cannot edit it directly.

What are Labelled loops?

What is a labeled loop in Java? A label is a valid variable name that denotes the name of the loop to where the control of execution should jump. To label a loop, place the label before the loop with a colon at the end. Therefore, a loop with the label is called a labeled loop.

What is labeled continue in Java?

In the above example, the labeled continue statement is used to skip the current iteration of the loop labeled as first . if (i==3 || j==2) continue first; Here, we can see the outermost for loop is labeled as first , first: for (int i = 1; i < 6; ++i) {..}


3 Answers

Here, scan: is simply a label. The break <label> syntax allows one to break out of outer loops, and to simulate some forms of the goto statement. The syntax is documented in the JLS:

A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; this statement, which is called the break target, then immediately completes normally. In this case, the break target need not be a switch, while, do, or for statement.

like image 170
NPE Avatar answered Oct 19 '22 18:10

NPE


It is a labeled block. where scan: is a label. It is commonly used when breaking/continue in case you have multiple loops. In this case break scan; simply breaks outta the labeled block(scan) when executed.

like image 44
PermGenError Avatar answered Oct 19 '22 19:10

PermGenError


You can set a label to break / or continue from within multiple loops deep.

Example

 outer:
 for(int i=...){
   for(int j=..){
     ...
     break outer; // leaves both loops   

   } 

 }
like image 2
MrSmith42 Avatar answered Oct 19 '22 18:10

MrSmith42