0
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

tumisma
  • 375
  • 3
  • 14
kurumkan
  • 2,635
  • 3
  • 31
  • 55
  • 2
    Possible duplicate of ["loop:" in Java code. What is this, why does it compile?](http://stackoverflow.com/questions/3821827/loop-in-java-code-what-is-this-why-does-it-compile) – Patrick Mar 03 '16 at 12:54
  • Possible duplicate of [Please explain the usage of Labeled Statements](http://stackoverflow.com/questions/2710422/please-explain-the-usage-of-labeled-statements) – andrucz Mar 03 '16 at 12:54

4 Answers4

4

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.

Eric Galluzzo
  • 3,191
  • 1
  • 20
  • 20
1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

You have defined a label called http. It is useless here but legal.

See Oracle's tutorial.

Emzor
  • 1,380
  • 17
  • 28
Xvolks
  • 2,065
  • 1
  • 21
  • 32
0

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.

Community
  • 1
  • 1
rubikonx9
  • 1,403
  • 15
  • 27