Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a label a Java statement or not?

Tags:

java

jls

Is a label a Java statement or not and if a label is a statement where is it defined as a statement in the Java Language Specification?

My question relates to the following reply of Jan Lahoda in the bug report that I've sent to Oracle. I'm unable to discuss it there because I can't get an account in the OpenJDK Jira.

https://bugs.openjdk.java.net/browse/JDK-8211052

In case of e.g.: A: B: while (true) continue A; the statement on which the "continue" is applied is not "while (true) continue A;", but "B: while (true) continue A;", and the spec requires the target for continue is a while/do/for statement, which is not fulfilled here. Hence the compile-time error.

I think a label in Java is not a statement and in Jan's example both the A and the B labels relate to the same while-loop statement and then no compile time error should be triggered.

Addition:

Isn't a labeled while/do/for statement in Java a while/do/for statement?

like image 218
Rostislav Krasny Avatar asked Jan 27 '23 09:01

Rostislav Krasny


1 Answers

Following the JLS a Statement can be

StatementWithoutTrailingSubstatement
LabeledStatement
IfThenStatement
IfThenElseStatement
WhileStatement
ForStatement

with LabeledStatement being

Identifier : Statement

and stated

The Identifier is declared to be the label of the immediately contained Statement

so in case of a loop like

public void method() {
    loop1: loop2: while (true) {
        if (true) {
            break loop1;
        } else {
            continue loop1;
        }
    }
}

the Statement of the Label loop1 is the entire loop2 ... stuff.

And if we look at the definition of break it is stated that

the break target need not be a switch, while, do, or for statement.

whereas for the continue it states

The continue target must be a while, do, or for statement, or a compile-time error occurs.

These definitions are properly in line with the compiler giving a compiler error for the continue loop1 statement and letting break loop1 be valid since loop2: ... is not a "while, do, or for statement".


Regarding your actual question: Is label a Java statement or not?

The following code is perfectly legal and compiles fine

public void method() {
    loop:;
}

This follows the expansion of Statement -> LabeledStatement -> Identifier : Statement -> loop : Statement -> loop : StatementWithoutTrailingSubstatement -> loop : EmptyStatement ->

loop : ;

No, a label is no statement by itself, an identifier (which is then called "label") plus a column plus an EmptyStatement (;) however is.


Isn't labeled while/do/for statement in Java a while/do/for statement?

No!

A LabeledStatement is exactly that: a LabeledStatement. A labeled while is Statement -> Label -> Identifier : Statement -> Identifier : WhileStatement which is something fundamentally different than Statement -> WhileStatement!

like image 80
luk2302 Avatar answered Jan 31 '23 07:01

luk2302