Search code examples
javalabelgoto

Code labels in Java


Here you can find that at its initial phases Java provided the goto instruction. Wisely, this instruction is not longer implemented. Nevertheless, as I discovered few minutes ago in other SO question, labels do still exist.

int x;
letsassign: x = 3;

I wonder why they are still there. What other uses do labels have apart from goto label?


Solution

  • It is for sportly multiple loopings:

    outer: for (;;) {
        while (true) {
            if (brr) {
                break outer;
            }
        }
        roo();
    }
    

    For break and continue.

    Java was designed to clean things up; after C++. However one did not want to risk outcries of "I cannot longer do that efficiently."