Java中的标记语句块? [英] Labeled Statement block in 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:
带有标签 Identifier
的 break
语句试图将控制转移到与它具有相同 Identifier
的封闭标记语句(第 14.7 节).标签;此语句称为中断目标,然后立即正常完成.在这种情况下,中断目标不必是 switch
、while
、do
或 for
语句.
A
break
statement with labelIdentifier
attempts to transfer control to the enclosing labeled statement (§14.7) that has the sameIdentifier
as its label; this statement, which is called the break target, then immediately completes normally. In this case, the break target need not be aswitch
,while
,do
, orfor
statement.
这篇关于Java中的标记语句块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!