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

M.A.Murali picture M.A.Murali · Mar 3, 2012 · Viewed 11.8k times · Source

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?

Answer

user529758 picture user529758 · Mar 3, 2012

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.