Hackerdarshi Hackerdarshi - 1 year ago 45
Java Question

Why does a HTTP URL in Java compile?

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?

Answer Source

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.