Why does Java not show an error for double semicol

2019-01-19 14:08发布

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?

6条回答
Lonely孤独者°
2楼-- · 2019-01-19 14:17

As told by other answers, usually the second semicolon is interpreted as an empty statement, which is permissible where ever a statement is permissible.

Actually, there are cases where a double semicolon does produce an error:

public int method() {
   return 1;;
}

When the compiler determines that a location is not reachable (and this is defined exactly in the JLS, but includes the locations directly after a return, break, continue and throw), no statement is allowed there, not even an empty one.

查看更多
别忘想泡老子
3楼-- · 2019-01-19 14:23

According to the Java language standard, the second semicolon is an empty statement.

An empty statement does nothing.

EmptyStatement:
    ;

Execution of an empty statement always completes normally.

查看更多
老娘就宠你
4楼-- · 2019-01-19 14:29

The semicolon ends the sentence.

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

The second semicolon means the sentence is empty.

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

Is wrong because you're trying to finish an uncompleted sentence.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-19 14:30

; by itself is an empty operator, so you effectively have two operators in the original case.

查看更多
The star\"
6楼-- · 2019-01-19 14:33

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.

查看更多
Juvenile、少年°
7楼-- · 2019-01-19 14:37

Because it is not error? Why you are asking about java? This is in most languages with similar syntax...

查看更多
登录 后发表回答