语法错误:带有多个分号的Import语句(Syntax Error: Import statement with multiple semi

编程入门 行业动态 更新时间:2024-10-25 03:18:56
语法错误:带有多个分号的Import语句(Syntax Error: Import statement with multiple semi-colon)

这可能是一个奇怪的问题,但却是一个有效的问题。 我知道下面的语句没有编译错误:

ArrayList list = new ArrayList();;; //(with three `;` semi-colon)

好的,现在我写了下面的import语句:

import java.util.ArrayList;;; (with three `;` semi-colon)

但是我得到了以下编译错误:

Syntax error on token ";", invalid StaticImportOnDemandDeclarationName

为什么?

This might be a weird question, but a valid one. I know below statement does not have compilation error:

ArrayList list = new ArrayList();;; //(with three `;` semi-colon)

Okay now I have written below import statement:

import java.util.ArrayList;;; (with three `;` semi-colon)

But I got below compilation error:

Syntax error on token ";", invalid StaticImportOnDemandDeclarationName

Why?

最满意答案

你有什么:

ArrayList list = new ArrayList();;; //(with three `;` semi-colon)

不是以三个分号结束的声明。 这是一个由一个分号结束的声明,后面是两个空的声明

空语句在Java中是合法的,但Java源文件的导入部分不包含语句,它由导入声明组成。

JLS 14.6定义了空语句:

空语句什么都不做。

EmptyStatement: ;

执行空语句总是正常完成。

可能合法使用空语句:

//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 ; ...

What you have here:

ArrayList list = new ArrayList();;; //(with three `;` semi-colon)

is not a statement terminated with three semi-colons. It's a statement terminated by one semi-colon, followed by two empty statements.

An empty statement is legal in Java, but the import section of a Java source file is not comprised of statements, it's comprised of import declarations.

JLS 14.6 defines the Empty Statement:

An empty statement does nothing.

EmptyStatement: ;

Execution of an empty statement always completes normally.

A perhaps-legitimate use of the empty statement:

//loop forever while (true) {;} // the body of this loop is an empty statement.

In other words, in your first example, you have an assignment followed by two empty statements. The semicolons are not strictly superfluous.

The import section has its own grammar, and none of the grammar rules allow for an arbitrary semi-colon. The grammar is specified by JLS 7.5:

ImportDeclaration: SingleTypeImportDeclaration TypeImportOnDemandDeclaration SingleStaticImportDeclaration StaticImportOnDemandDeclaration SingleTypeImportDeclaration: import TypeName ; ...

更多推荐

本文发布于:2023-08-03 01:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1382469.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   分号   语句   语法错误   statement

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!