Somewhere going through java good articles, I found that such code compiles perfectly.
public int myMethod(){
http://www.google.com
return 1;
}
description says that http: word will be treated as label and //www.google.com as comment
I am not getting how Java Label is useful outside loop?
Under what situation Java Label outside loop should be used?
解决方案
Here is one benefit of using labels in Java:
block:
{
// some code
if(condition) break block;
// rest of code that won't be executed if condition is true
}
Another usage with nested-loops:
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
}
or:
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
}