tags:

views:

224

answers:

8

This is a syntax question.

I was looking at some open source files and I met some syntax that I cannot recognize and I was hoping you could clear it up for me.

(This is taken from the Main.java in the Rhino debugger here)

public static String[] processOptions(String args[])
    {
        String usageError;
        goodUsage: for (int i = 0; ; ++i) {
            if (i == args.length) {
                return new String[0];
            }
            String arg = args[i];
            if (!arg.startsWith("-")) {
                processStdin = false;
                fileList.add(arg);
                String[] result = new String[args.length - i - 1];
                System.arraycopy(args, i+1, result, 0, args.length - i - 1);
                return result;
            }
            if (arg.equals("-version")) {
                if (++i == args.length) {
                    usageError = arg;
                    break goodUsage;
                }
                int version;
                try {
                    version = Integer.parseInt(args[i]);
                } catch (NumberFormatException ex) {
                    usageError = args[i];
                    break goodUsage;
                }

My question is what is goodUsage? what is the name of this syntax, and what is it used for?

+3  A: 

This is just a branching statement and goodUsage is simply a label. See here:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/branch.html

BobbyShaftoe
+1  A: 

Its a label - the break statement supplies the label and this means it will be treated like a goto except that in Java a "break to label" will take you to the end of the block declared after the label.

Andrew Hare
+15  A: 

They are labels. They are used to be able to break out of an inner block into a block that is not the one immediately surrounding it.

Here is the relevant part of the Java Language Specification that deals with labels.

You likely haven't seen it before because 99% of the time code can be rewritten to not use such a thing, and it's probably a sign that the method is doing too much.

(Also I should mention for anyone that happens to come across this question/answer from a search engine, it isn't new syntax - it's been around since the version one of the JLS.)

matt b
100% of the time it can be re-written not to use them, 99.9% (Maybe that should be + .1%) the re-written version is more clear.
Bill K
This description emphasizes the irony in the choice of label name.
EmFi
Labeled breaks are often useful when you *must* have a `for()` loop several layers deep. It often makes no sense to break out the inner `for()` loop into another method if you're iterating over a 2D or 3D array.
jprete
@jprete I would consider that case the 1%
matt b
+1  A: 

That's called a label. It lets you specify which loop you're breaking out of.

Kaivosukeltaja
A: 

It is a labled break statement

Nick
A: 

It's a label, see wikepedia article

Gaël Marziou
+2  A: 

It is called a labeled break. It can be used to specify which loop you want to break instead of the innermost as it would normally do. Here is a good example: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/branch.html

Mathieu L
+3  A: 

Makes me think of the following brain-teaser:

public class Main {
    public static void main(String[] args) {
        http://stackoverflow.com
        System.out.println("Does it?");
    }
}

Does it compile?

Edit: SO's syntax highlighter does its job too good!

Bart Kiers
nice catch...! ;)
p3t0r
totally, thanks :)
special0ne