Why is the method body filled with valid semicolon syntax?

public static void main(String[] args) {
        System.out.println("World Hello!");;;;;;;;;;;;;;
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        ;;;;;;
               ;;;;;;;;;
               ;;;;;;;
               ;;;;;;;;;;;;;; 
               ;;;;;
}

Normal person - this is what I want to say: "What do all these semicolons do?"

+4
source share
3 answers

Because a simple semicolon next to another semicolon is an empty statement. Thus, many half-columns next to each other - many empty statements. It compiles and runs without problems.

+11
source

According to Oracle Java Language Specification :

An empty instruction does nothing.

EmptyStatement:
    ;

The execution of an empty instruction always runs normally.

, , .

-, , , ";"

// Stack: 1, Locals: 1
public Duh1();
0  aload_0 [this]
    1  invokespecial java.lang.Object() [8]
    4  return
      Line numbers:
        [pc: 0, line: 3]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: Duh

  // Method descriptor #15 ([Ljava/lang/String;)V
  // Stack: 2, Locals: 1
  public static void main(java.lang.String[] args);
    0  getstatic java.lang.System.out : java.io.PrintStream [16]
    3  ldc <String "World Hello!"> [22]
    5  invokevirtual java.io.PrintStream.println(java.lang.String) : void [24]
    8  return
      Line numbers:
        [pc: 0, line: 5]
        [pc: 8, line: 15]
      Local variable table:
        [pc: 0, pc: 9] local: args index: 0 type: java.lang.String[]

Line numbers:
        [pc: 0, line: 5]
        [pc: 8, line: 6]

1 ";"

// Method descriptor #6 ()V
  // Stack: 1, Locals: 1
  public Duh2();
    0  aload_0 [this]
    1  invokespecial java.lang.Object() [8]
    4  return
      Line numbers:
        [pc: 0, line: 3]
      Local variable table:
        [pc: 0, pc: 5] local: this index: 0 type: Duh2

  // Method descriptor #15 ([Ljava/lang/String;)V
  // Stack: 2, Locals: 1
  public static void main(java.lang.String[] args);
    0  getstatic java.lang.System.out : java.io.PrintStream [16]
    3  ldc <String "World Hello!"> [22]
    5  invokevirtual java.io.PrintStream.println(java.lang.String) : void [24]
    8  return
      Line numbers:
        [pc: 0, line: 5]
        [pc: 8, line: 6]
      Local variable table:
        [pc: 0, pc: 9] local: args index: 0 type: java.lang.String[]

Line numbers:
        [pc: 0, line: 5]
        [pc: 8, line: 6]

, - ";" ";".

+7

Because semicolony only symbolizes a new line. This means that we can simply write:

cout << "Hello World\n"; return 0;

Without writing on a new line. Keep in mind that it is more accurate for writing on separate lines.

0
source

Source: https://habr.com/ru/post/1534550/


All Articles