javajava-compiler-api

Why does Java not show an error for double semicolon at the end of a statement?


I accidentally wrote a java statement with two semicolons at the end. The java compiler does not show any error and it runs.

Code:

System.out.println("Length after delete the text is "+name.length());;

For learning purposes I tried adding different characters after the semicolon, and the java compiler has shown the compile time error as Syntax error on token ")", delete this token.

This statement:

System.out.println("Length after delete the text is "+name.length());)

Why does java treat the semicolon and other characters as different?


Solution

  • Because a double semicolon is not treated as a double semicolon but as a semicolon plus an empty statement. And an empty statement, which does nothing, is not an error.