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!?"
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.
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 ";".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With