加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

标签是否是Java语句?

发布时间:2020-12-15 08:27:26 所属栏目:Java 来源:网络整理
导读:标签是否是 Java语句,标签是否是在 Java语言规范中定义为语句的语句? 我的问题与Jan Lahoda在我发给Oracle的错误报告中的以下回复有关.我无法在那里讨论,因为我无法在OpenJDK Jira中获得帐户. https://bugs.openjdk.java.net/browse/JDK-8211052 In case of
标签是否是 Java语句,标签是否是在 Java语言规范中定义为语句的语句?

我的问题与Jan Lahoda在我发给Oracle的错误报告中的以下回复有关.我无法在那里讨论,因为我无法在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.

我认为Java中的标签不是一个语句,在Jan的例子中,A和B标签都与同一个while循环语句相关,因此不应该触发编译时错误.

加成:

在Java中/ while / do / for语句中是不是标记为/ do / for语句?

解决方法

在 JLS之后,声明可以

StatementWithoutTrailingSubstatement
LabeledStatement
IfThenStatement
IfThenElseStatement
WhileStatement
ForStatement

与LabeledStatement一样

Identifier : Statement

并说明

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

所以在循环的情况下

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

Label的语句loop1是整个loop2 ……的东西.

如果我们看一下break的定义,就说明了

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

而对于继续它说

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

这些定义与编译器一致,为continue loop1语句提供编译器错误,并且让break loop1有效,因为loop2:…不是“while,do或for语句”.

关于你的实际问题:标签是否是Java声明?

以下代码完全合法,编译良好

public void method() {
    loop:;
}

这遵循Statement – >的扩展. LabeledStatement – >标识符:声明 – > loop:Statement – > loop:StatementWithoutTrailingSubstatement – > loop:EmptyStatement – >

loop : ;

不,标签本身没有声明,标识符(后来称为“标签”)加上colom加上EmptyStatement(;)然而.

在/ a / do / for语句中没有标记/ do / for语句吗?

没有!LabeledStatement就是这样的:LabeledStatement. A标记为声明 – >标签 – >标识符:声明 – >标识符:WhileStatement与Statement基本不同 – > WhileStatement!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读