Полезная информация

Java Language Reference

Previous Chapter 6
Statements and Control Structures
Next
 

6.2 Labeled Statements

Zero or more labels can appear before a statement:

[Graphic: Figure from the text]

A label is defined throughout the block in which it occurs. The names of labels are independent of all other kinds of names. In other words, if a label has the same name as a local variable, formal parameter, class, interface, field variable, or method, there is never any confusion or interaction between those names.[1] For example, the following code works even though it contains a label and formal parameter with the same name:

[1] Prior to version 1.0.2, Java required labels to have names that did not conflict with the names of local variables or formal parameters.

public static void main (String[] argv) {
  argv:
    while (true) {
        System.out.println(argv[0]);
        if ( Math.random() >.4)
            break argv;
        System.out.println("Again");
    }
}

Labels are used to mark statements, but a labeled statement does not affect the order of execution when it is defined. The statement following the label is executed as if the label were not present. However, a label can be used in a break or continue statement to transfer control to a labeled statement. Unlike C/C++, Java does not have a goto statement.

References Identifiers; Statement 6; The break Statement; The continue Statement


Previous Home Next
Blocks Book Index The Empty Statement

Java in a Nutshell Java Language Reference Java AWT Java Fundamental Classes Exploring Java