Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a method body filled with semicolons acceptable syntax?

Tags:

java

methods

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!?"

like image 582
Zeratas Avatar asked Apr 01 '14 23:04

Zeratas


2 Answers

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.

like image 133
Luiggi Mendoza Avatar answered Nov 03 '22 01:11

Luiggi Mendoza


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 ";".

like image 44
dreamcrash Avatar answered Nov 03 '22 01:11

dreamcrash