Using Java Label

Somewhere, going through java good articles, I found that such code compiles fine.

public int myMethod(){
    http://www.google.com
    return 1;
}

the description says that the word http:will be treated as a tag and //www.google.comas a comment

I do not get how Java Label is useful outside the loop? In what situation should an external Java Label loop be used?

+4
source share
2 answers

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
}

Other uses 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
}
+7
source

Just because it compiles does not mean that it is useful .....

Java ( break continue...?). , , , .

0

All Articles