Zeratas
Zeratas

Reputation: 1015

Why is a method body filled with semicolons acceptable syntax?

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

The normal person is me wants to say: "What are all of those semicolons doing there!?"

Upvotes: 5

Views: 346

Answers (3)

dreamcrash
dreamcrash

Reputation: 51513

According to Oracle Java Language Specification:

An empty statement does nothing.

EmptyStatement:
    ;

Execution of an empty statement always completes normally.

Since the language it self allows consecutive semicolons, the compiler will not produce an error.

You could also check the bytecode to see what is happening, the code with a lot of ";" :

// 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]

and the code with only 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]

As you can check the bytecode is exactly the same for both version with single ";" and consecutive multiple ";".

Upvotes: 7

Kinggadino
Kinggadino

Reputation: 47

Because a semi-colon only symbolises a newline. This means that we can simpily write:

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

Without even writing on a newline. Keep in mind its more tidy to write on seperate lines.

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85789

Because a simple semicolon next to another semicolon is an empty statement. So, lot of semi colons next to each other are lot of empty statements. It compiles and runs with no problems.

Upvotes: 11

Related Questions