12

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.

Falkenfighter
  • 568
  • 6
  • 16

4 Answers4

18

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Is this practice as reviled as goto's? – Daniel Jan 03 '13 at 21:55
  • 2
    @Daniel: I don't want to start any religious debates. ;) However, since this form can be considered more "structured" than a raw `goto`, I would imagine some anti-`goto` advocates might consider it less objectionable. – NPE Jan 03 '13 at 21:56
  • 1
    @Daniel no, because you can't use it to arbitrarily jump around in code. It's useful with `break` in nested loops to specify which loop to break out of. – Alex Jan 03 '13 at 21:56
  • @NPE: Your link answered my question perfectly and totally explains why I was unable to find a scan block! Thanks again – Falkenfighter Jan 03 '13 at 22:13
2

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.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
2

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   

   } 

 }
MrSmith42
  • 9,961
  • 6
  • 38
  • 49
1

It is a label. It is a indicator for flow control.

If you look at your code, you see below

 break scan;

When this happens, the flow exits completely the scan block.

By the way, it can be any identifier, scan is not a keyword at all.

SJuan76
  • 24,532
  • 6
  • 47
  • 87