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

守給你的承諾、 提交于 2019-11-28 12:05:08

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.

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.

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.

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!