0

Here is an interesting piece of code I came across: It looks quite strange but it does compile.

public static void main(String[] args) {        
    http://servername:port/index.html
    System.out.println("Hello strange world");
}

Thoughts?

cmd
  • 11,622
  • 7
  • 51
  • 61

3 Answers3

9

http: is a label. The rest is just an inline comment //.

One can ask, why do we have labels in Java?

First thought: goto. But, goto implementation has been removed from Java as needless. The only thing which stayed is a reserved keyword goto. Read more here.

So, what's the usage of labels?

Labels can be used with break statement. Example from documentation, with search label:

class BreakWithLabelDemo {
    public static void main(String[] args) {

        int[][] arrayOfInts = { 
            { 32, 87, 3, 589 },
            { 12, 1076, 2000, 8 },
            { 622, 127, 77, 955 }
        };
        int searchfor = 12;

        int i;
        int j = 0;
        boolean foundIt = false;

    search:
        for (i = 0; i < arrayOfInts.length; i++) {
            for (j = 0; j < arrayOfInts[i].length; j++) {
                if (arrayOfInts[i][j] == searchfor) {
                    foundIt = true;
                    break search;
                }
            }
        }

        if (foundIt) {
            System.out.println("Found " + searchfor + " at " + i + ", " + j);
        } else {
            System.out.println(searchfor + " not in the array");
        }
    }
}
Community
  • 1
  • 1
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
3

A label followed by an inline coment

Yes, Java has label

public static void main(String[] args) {
    hello:
}

And of course online comments

public static void main(String[] args) {
    int i; // this is a comment.
}
SJuan76
  • 24,532
  • 6
  • 47
  • 87
0

http: is a label

//servername:port/index.html is a comment.

james_bond
  • 6,778
  • 3
  • 28
  • 34