Java goto Keyword
Usage and Examples

goto Keyword in Java

Java has a reserved word called goto, but here's the twist—it’s not actually used in the language. That might sound strange at first, but there's a very intentional reason behind it. The designers of Java chose to reserve goto as a keyword to maintain flexibility for future use, yet they also decided to avoid including it in the language’s control flow structure due to its problematic history in older languages like C.

Why is goto Reserved but Unused?

In Java, goto is a reserved keyword, which means you can't use it as a variable name, method name, class name, or anything else. It’s a placeholder, set aside just in case the language designers ever wanted to implement it later. But the likelihood of that happening is extremely low.

// This will cause a compilation error
int goto = 5; // Error: 'goto' is a reserved word
error: as of release 1.3, 'goto' is a keyword, and may not be used as an identifier

The goto statement was historically used to jump to arbitrary lines of code, which often led to what is called "spaghetti code"—difficult to read, error-prone, and hard to maintain.

What to Use Instead of goto in Java?

Java offers several structured control flow mechanisms that serve the same purpose more safely and clearly:

  • break – exit from loops or switch blocks
  • continue – skip the current loop iteration
  • return – exit from a method
  • throw and try-catch – handle unexpected events or errors
  • labeled break and labeled continue – for nested loops (a safer form of limited goto)

Using labeled break – The Java Way

Here’s how Java handles situations that might have used goto in older languages:

public class LabeledBreakExample {
  public static void main(String[] args) {
    outerLoop:
    for (int i = 1; i <= 3; i++) {
      for (int j = 1; j <= 3; j++) {
        if (i * j == 4) {
          break outerLoop;
        }
        System.out.println("i: " + i + ", j: " + j);
      }
    }
    System.out.println("Exited the loop using labeled break.");
  }
}
i: 1, j: 1
i: 1, j: 2
i: 1, j: 3
i: 2, j: 1
Exited the loop using labeled break.

In the example above, we used a label outerLoop: to refer to the outer loop. The break outerLoop; acts like a controlled jump—safe, predictable, and within a structured block.

Using continue with Labels

Similarly, Java lets you use labels with continue to skip iterations:

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

In this case, when j == 1, the inner loop is skipped, and control continues from the start of the outer loop. Again, a safe and readable substitute for what a goto might have attempted.

Conclusion

While Java includes the goto keyword in its list of reserved words, it has no actual function in the language. Its existence is mostly historical. The Java language promotes clarity, structure, and maintainability—and omitting goto is part of that design philosophy.

Instead of longing for goto, embrace the robust alternatives Java provides—structured control flow makes your code more readable and less error-prone.

If you're coming from older programming languages, it's a shift, but one well worth making.

Quick Recap

  • goto is reserved in Java but not implemented
  • It cannot be used as an identifier
  • Use break, continue, return, or labeled loops instead
  • Java favors structured programming for better readability and maintainability