这可能是一个奇怪的问题,但却是一个有效的问题。我知道下面的语句没有编译错误:
ArrayList list = new ArrayList();;; //(with three `;` semi-colon)
好的,现在我已经写了下面的导入语句:
import java.util.ArrayList;;; (with three `;` semi-colon)
但是我遇到了以下编译错误:
Syntax error on token ";", invalid StaticImportOnDemandDeclarationName
为什么?
最佳答案
这里有什么:
ArrayList list = new ArrayList();;; //(with three `;` semicolons)
不是以三个分号结尾的语句。这是一个以一个分号结尾的语句,后跟两个空语句。
空语句在 Java 中是合法的,但 Java 源文件的导入部分不包含语句,它由导入声明组成。
JLS 14.6定义空语句:
An empty statement does nothing.
EmptyStatement: ;
Execution of an empty statement always completes normally.
空语句的可能合法使用:
//loop forever
while (true) {;} // the body of this loop is an empty statement.
换句话说,在您的第一个示例中,您有一个赋值后跟两个空语句。分号并非严格多余。
import
部分有自己的语法,没有任何语法规则允许使用任意分号。语法由 JLS 7.5 指定:
ImportDeclaration:
SingleTypeImportDeclaration
TypeImportOnDemandDeclaration
SingleStaticImportDeclaration
StaticImportOnDemandDeclaration
SingleTypeImportDeclaration:
import TypeName ;
...
关于java - 语法错误 : Import statement with multiple semi-colon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25352929/