Java中的标记语句块? [英] Labeled Statement block in Java?

查看:23
本文介绍了Java中的标记语句块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我发现一段代码被 scan: {} 块包围时,我正在浏览一些基本的 Java 对象.以下代码来自 String 类中的 toLowerCase() 方法.

I was browsing through some of the base Java objects when I found a section of code surrounded by a scan: {} block. The following code is from the toLowerCase() method inside the String class.

scan: {
            for (firstUpper = 0 ; firstUpper < len; ) {
                char c = value[firstUpper];
                if ((c >= Character.MIN_HIGH_SURROGATE)
                        && (c <= Character.MAX_HIGH_SURROGATE)) {
                    int supplChar = codePointAt(firstUpper);
                    if (supplChar != Character.toLowerCase(supplChar)) {
                        break scan;
                    }
                    firstUpper += Character.charCount(supplChar);
                } else {
                    if (c != Character.toLowerCase(c)) {
                        break scan;
                    }
                    firstUpper++;
                }
            }
            return this;
        }

有人可以解释一下 scan:{} 块的用途以及该语法的来源吗?除非在三元运算符中使用,否则我还没有在 Java 中看到这样的单词后面有冒号.

Could someone please explain what the scan:{} block is used for and where this syntax comes from? I've yet to see a colon after a word like this in Java unless used in a ternary operator.

谢谢!

更新标题以正确匹配已回答的问题.

Updated title to correctly match answered question.

推荐答案

这里,scan: 只是一个 标签.break 语法允许跳出外部循环,并模拟某些形式的 goto 语句.语法记录在 JLS:

Here, scan: is simply a label. The break <label> syntax allows one to break out of outer loops, and to simulate some forms of the goto statement. The syntax is documented in the JLS:

带有标签 Identifierbreak 语句试图将控制转移到与它具有相同 Identifier 的封闭标记语句(第 14.7 节).标签;此语句称为中断目标,然后立即正常完成.在这种情况下,中断目标不必是 switchwhiledofor 语句.

A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; this statement, which is called the break target, then immediately completes normally. In this case, the break target need not be a switch, while, do, or for statement.

这篇关于Java中的标记语句块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆