special0ne
special0ne

Reputation: 6263

What is this syntax in Java? I do not recognize it

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?

Upvotes: 3

Views: 446

Answers (8)

Bart Kiers
Bart Kiers

Reputation: 170308

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!

Upvotes: 4

matt b
matt b

Reputation: 140061

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

Upvotes: 16

user197643
user197643

Reputation:

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

Upvotes: 2

Gaël Marziou
Gaël Marziou

Reputation: 16294

It's a label, see wikepedia article

Upvotes: 0

Nick
Nick

Reputation: 3347

It is a labled break statement

Upvotes: 0

Kaivosukeltaja
Kaivosukeltaja

Reputation: 15735

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

Upvotes: 1

Andrew Hare
Andrew Hare

Reputation: 351748

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.

Upvotes: 1

BobbyShaftoe
BobbyShaftoe

Reputation: 28499

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

Upvotes: 3

Related Questions