2

In the source code I forgot to add comment before the link. For example, the following code:

public class HelloWorld
{
  public static void main(String[] args)
  {
    http://www.example.com/random-link-here
    System.out.println("Hello World!");
  }
}

Why it compiles? Somehow unable to figure out what it does... Its like label for Goto + comment, but Java does not have a goto...

Maris B.
  • 2,189
  • 3
  • 20
  • 34

3 Answers3

6

The http: part is a label labelling the statement that follows. The // part introduces a line comment.

Normally you see labelled statements in directed break situations, like:

outer:
for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
         if (someConditionThatNeedsToTakeUsOutOfBothLoops) {
             break outer;
         }
    }
}

...however any statement may have a label. In your case you've labelled the System.out.println("Hello World!"); statement.

T.J. Crowder
  • 959,406
  • 173
  • 1,780
  • 1,769
5

Because :

http:   //www.example.com/random-link-here
^^^^    ^--------------------------------^

http: is a label and the rest is a comment

YCF_L
  • 51,266
  • 13
  • 85
  • 129
1

This is a famous Java Puzzler. It is a label for a goto with a comment. Java reserves the goto keyword, but does not actually implement the goto statement. Look at Puzzler 22 from http://www.javapuzzlers.com/ as a related example.