Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

goto — reserved unused keyword

goto — reserved unused keyword: goto is a reserved but unused Java keyword: the parser accepts it neither as an identifier nor as a jump statement.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

goto

goto is a reserved but unused Java keyword: the parser accepts it neither as an identifier nor as a jump statement.

Express flow with loops, methods, return, and labeled break or continue; a label can exit a selected enclosing loop.

Do not emulate arbitrary jumps with sprawling flags, because they obscure invariants and exception-handling paths.

A containing name such as gotoLabel is a legal identifier, but the exact token goto always remains reserved.

Labeled break outer exits both loops at the third increment; the legal identifier gotoLabel prints exactly gotoLabel=3.

Examples

public class Main {
    public static void main(String[] args) {
        int gotoLabel = 0;
        outer:
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 2; j++) {
                gotoLabel++;
                if (gotoLabel == 3) break outer;
            }
        }
        System.out.println("gotoLabel=" + gotoLabel);
    }
}

Result

gotoLabel=3

Labeled break outer exits both loops at the third increment; the legal identifier gotoLabel prints exactly gotoLabel=3.

Sources