22

If you have a program like this:

public class ABC
{
    public static void main(String args[])
    {
        System.out.println("1");
        http://example.com
        System.out.println("2");
    }
}

Note the URL http://example.com written in between the two output statements.

Why does the program compile without any errors?

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
dryairship
  • 6,022
  • 4
  • 28
  • 54
  • 1
    I was wondering about this too, when once wrote the link between lines and forgot about `//`, labels are funny :) – coolguy Jan 03 '16 at 12:36
  • I can remember this trick from Java 1.0 I learned at university, unfortunately I only have a heavily battered "Core Java" for 1.1 instead of 1.0 :P Looked for dupes, but couldn't find any. – Maarten Bodewes Jan 03 '16 at 13:26
  • Not sure why it was marked duplicate. The question **Java Label Usage** has a different question, and its answers are pertaining to that question. Also, it was marked a duplicate of a question which itself is a duplicate! :/ – dryairship Jan 04 '16 at 12:14
  • The proper duplicate would be this one http://stackoverflow.com/questions/916871/how-can-this-java-code-compile (which I apparently missed when reviewing) – Tunaki Apr 17 '16 at 13:43
  • @Hackerdarshi Based on my experience in SO, direct title which straight to the point doesn't make an excuse to mark duplicated if the point existed in another answer which has general title like "Why my code doesn't works". Sometime nobody care to mark it, sometime it did, when this happen, no point you complain it. – 林果皞 Jun 01 '16 at 18:45

3 Answers3

36

The reason the program compiles without error is that the program considers http: as a label, which is allowed in Java, and is mostly used with loops.
The second part, i.e., //example.com is a comment, due to the //, and is therefore ignored by the compiler.

Hence it compiles properly.

hichris123
  • 10,145
  • 15
  • 56
  • 70
dryairship
  • 6,022
  • 4
  • 28
  • 54
  • 4
    Still a bad form, should be mentioned. – John Dvorak Jan 03 '16 at 12:24
  • 4
    Is it allowed to do this kind of "I ask a question and immediatly answer it"? oO Ähm.. OP did this in a couple of his questions on this site... – Flocke Jan 03 '16 at 12:30
  • 1
    @Flocke yes, it's okay at this site :) – coolguy Jan 03 '16 at 12:33
  • 7
    When you are asking a question, you might have seen this written below: `Answer your own question – share your knowledge, Q&A-style`. I ticked on it, and did it. – dryairship Jan 03 '16 at 12:33
  • 11
    But still this seems like some reputation stunt – aliasm2k Jan 03 '16 at 12:35
  • 6
    @Flocke This is my first question like this. The other question you are talking about got its answer in a comment. That gave me my answer. But the user did not convert it into an answer. Then about a few days ago, I was reviewing my profile, when SO recommended that I should start a bounty on the question. But I had got my answer, so I wrote that as an answer for anyone who might search for it in the future. – dryairship Jan 03 '16 at 12:40
  • 1
    @aliasm2k As long as the information is informative and not a duplicate it's OK in my book. If you want to spend oodles of time looking for dupes on easy to answer questions then you "deserve" the rep :) – Maarten Bodewes Jan 03 '16 at 13:22
  • 2
    @aliasm2k It might to you, but it's [**explicitly encouraged**](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/) on the Stack network. – Angew is no longer proud of SO Jan 03 '16 at 14:27
  • 2
    @Flocke Not only allowed, it's even [encouraged](http://blog.stackoverflow.com/2011/07/its-ok-to-ask-and-answer-your-own-questions/). – Angew is no longer proud of SO Jan 03 '16 at 14:27
  • 3
    @Angew It's worth mentioning is that it is encouraged, but that it should **not** encourage you to create duplicates... – Marco13 Jan 03 '16 at 21:49
  • @Marco13 That's for sure. I was talking on general principles, not about this particular question. And I wasn't aware it was a duplicate at the time I made the comments. – Angew is no longer proud of SO Jan 04 '16 at 07:51
18

As it described in this answer, this code compiles because Java compiler thinks that http: is a label and everything after // is a comment.

In addition, this won't compile:

System.out.println("1");
http://example.com
int i = 1;

And this won't:

System.out.println("1");
http://example.com
Date date = new Date();

But this will:

System.out.println("1");
int i;
http://example.com
i = 1;

And this will:

int i = 0;
System.out.println("1");
http://example.com
i = i + 1;

And this:

int i = 0;
System.out.println("1");
http://example.com
i++;

So you can't declare the variable after the label. Also Intellij IDEA shows some warnings with code like this.

Community
  • 1
  • 1
coolguy
  • 3,011
  • 2
  • 18
  • 35
  • Please either write a full answer or integrate the examples in the existing answer. – Maarten Bodewes Jan 03 '16 at 13:19
  • 1
    @coolguy You should not delete your answer. It provides some good examples, that is why it is upvoted. :) – dryairship Jan 03 '16 at 13:28
  • Note that you can refer to http://stackoverflow.com/a/5058025/1074998 and http://stackoverflow.com/a/8145704/1074998 to know why declaration is not allow. – 林果皞 Jun 01 '16 at 20:18
1

Looks like compiler accepting only statements after labels. So reserved keywords and class names are not allowed. But there is an exception to this rule.

interface PrintSome {
    default void print() {
        System.out.println("I`m printing some!");
    }
}

and then:

http://www.example.com
new PrintSome(){}.print();

is compiling.

over9k
  • 115
  • 1
  • 10