public static void main(String args[])
{
http: //vk.com
System.out.println ("Hello world");
}
I'm wondering why this code doesn't throw any exceptions or errors. Can you provide me any documentation, that explains this case? Thank you
public static void main(String args[])
{
http: //vk.com
System.out.println ("Hello world");
}
I'm wondering why this code doesn't throw any exceptions or errors. Can you provide me any documentation, that explains this case? Thank you
This is valid because:
http:
is a label, that can be used with break
and continue
statements.//vk.com
is a comment.The rest is ordinary, valid Java syntax.
The body of the method parses as a labeled statement.
http: <-- label
//vk.com <-- comment
System.out.println ("Hello world"); <-- statement
In this case the label is redundant, but if the statement was (for example) a loop, then you could use a break http;
statement to break the loop.
For example
some_label: for (int i = 1; i < 100; i++) {
for (int j = 1; j < 100; j++) {
if (something(i, j)) {
break some_label;
}
}
}
Statement labels are so rarely used in Java, that a lot of programmers don't know what they mean. That is (IMO) a good reason not to use them.
This syntax:
whatever:
creates a label named whatever. It is usually meant to be used to control the loop flow, as in this answer.
Java does reserve the goto
keyword as well, but is not used.
This is then followed by a comment:
//vk.com
which has no reason to do anything.