1

Possible Duplicate:
What is a scan: block in Java?

I was browsing through some of the Java standard library code and I found the toUpperCase method in String.java (java.lang.String - JDK 1.6.0_32 x64). I've removed the irrelevant parts of the code in the snippet below.

The part that confused me was the scan: { ... } syntax. I am under the impression that lambda expressions aren't a part of SE 6 or 7. Can somebody please enlighten me?

// String.java - line 2547 onwards.
public String toUpperCase(Locale locale) {
    // Do stuff.

    /* Now check if there are any characters that need to be changed. */
    scan: {
        // Do stuff.
    }

    // Do more stuff.
}
Community
  • 1
  • 1
Chris Parton
  • 1,052
  • 9
  • 16
  • It might help you check the accepted answer http://stackoverflow.com/questions/3821827/loop-in-java-code-what-is-this-why-does-it-compile-and-generally-wtf – exexzian Jan 07 '13 at 21:13
  • @LouisWasserman: It certainly could be a duplicate. Searching for scan mainly brought up references to `java.util.Scanner`. – Chris Parton Jan 07 '13 at 21:18

3 Answers3

4
scan: { ... }

Its a Labeled Statements. usually labels are used in case of nested loop where you can either break or continue the loop.

Eg:

outer:while(cond) {
in:while(cond){
   break outer;
}
}
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • Ahh ok, that makes sense :) I was trying to Google it, but I couldn't think of the right search term (`java colon syntax` wasn't very helpful). Well, now that I know what it's called, I can do my own research on it. Thanks to you and Nambari for the links. +1 to you both for the speedy response. I'll give it 10 minutes, then accept this answer unless some crazy Jon Skeet-esque answer comes along. – Chris Parton Jan 07 '13 at 21:13
  • @ChrisParton now you know ... refer to JLS for more info :) – PermGenError Jan 07 '13 at 21:14
3

It is called labeled statements. scan is label here and the statement is contained in {...}.

As per Java Language Specification 14.7

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

kosa
  • 65,990
  • 13
  • 130
  • 167
  • Thanks for this! I love all of these little language features of Java. I learned C++ when I was in high school and the syntax has never thrown any surprises at me once I understood templates and so on. Java, on the other hand, throws syntactic curveballs like this. It certainly keeps me interested in the language. – Chris Parton Jan 07 '13 at 21:23
  • @ChrisParton: That is correct, but best places to start when you come across question would be start with javadoc and language specification. Most of the times you will get answer. Good luck. – kosa Jan 07 '13 at 21:25
3

You can have a Label (which is usually in UPPER_CASE) even if you don't have a loop. e.g.

scan: {
   if (condition) break;
   // do something
}

The only place I use this is to avoid a "found" flag

FOUND: {
    for(MyType mt: myTypeList)
       if(condtion(mt))
            break FOUND;
    // not found code here
}

If you want to write a confusing label you can do

http://www.google.com
if (searching) {

}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130