在阅读java优秀文章时,我发现这样的代码可以完美地编译。
public int myMethod(){
http://www.google.com
return 1;
}description表示http: word将被视为标签,而//www.google.com将被视为注释
我不明白Java标签在循环外是如何有用的?在什么情况下应该使用Java标签外循环?
发布于 2013-11-07 20:58:05
下面是在Java中使用标签的一个好处:
block:
{
// some code
if(condition) break block;
// rest of code that won't be executed if condition is true
}嵌套循环的另一种用法:
outterLoop: for(int i = 0; i < 10; i++)
{
while(condition)
{
// some code
if(someConditon) break outterLoop; // break the for-loop
if(anotherConditon) break; // break the while-loop
// another code
}
// more code
}或者:
outterLoop: for(int i = 0; i < 10; i++)
{
while(condition)
{
// some code
if(someConditon) continue outterLoop; // go to the next iteration of the for-loop
if(anotherConditon) continue; // go to the next iteration of the while-loop
// another code
}
// more code
}发布于 2013-11-07 20:59:20
仅仅因为它编译并不意味着它是有用的……
标签在Java中经常被忽略(谁使用带标签的break和continue ...?)。但是,不使用标签并不意味着它是非法的。
https://stackoverflow.com/questions/19836549
复制相似问题