Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Label usage [duplicate]

Tags:

java

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?

like image 821
Jayesh Avatar asked Nov 07 '13 12:11

Jayesh


People also ask

What is difference between label and JLabel?

The fundamental difference between the two is that JLabel allows the label to be composed of text, graphics, or both, while the old Label class only allowed simple text labels. This is a powerful enhancement, making it very simple to add graphics to your user interface.

Is label editable in Java?

It is impossible to make an editable label as it exists. You can use a jTextField with no border and same background as the JFrame. Having said that, you can add a KeyListener to your JLabel and update your label's text depending on the key that was pressed.

How do you remove labels in Java?

add(label); final JButton remove = new JButton("Remove label"); leftPanel. add(remove); add. addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { rightPanel. remove(label); } });


1 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
}

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
}
like image 78
Eng.Fouad Avatar answered Oct 06 '22 15:10

Eng.Fouad