jmlup
jmlup

Reputation: 91

Why is it allowed to label almost every statement in Java?

I understand that the main purpose of labels is to use them with break and continue to alter the usual behaviour of the loop. But it's possible to label every statement that is not a declaration.

int j = 0;
LABEL1: j++;
LABEL2: for (int i = 0; i < 4 ; i++) {
    if (i == 3) break LABEL2;
}

Is there any purpose to labels like LABEL1 since it's not allowed to break LABEL1?

Upvotes: 7

Views: 163

Answers (1)

Wim Coenen
Wim Coenen

Reputation: 66733

An early unreleased version of java used to have GOTO. In order to jump to any statement with GOTO, you have to be able to label it.

Then at some point James Gosling decided that it wasn't a good feature and ripped it out. This involved grepping through all java code that existed at the time and rewriting any GOTO usage; there were 13 uses. (Source: youtube video)

So, like GOTO still being a reserved word, it's a remnant of GOTO support.

Upvotes: 8

Related Questions