java 标记,用Java标记语句块?

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;

}

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.

Thanks!

Edit: Updated title to correctly match answered question.

解决方案

Here, scan: is simply a label. The break 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:

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值