gpt4 book ai didi

java - java类编写语法

转载 作者:行者123 更新时间:2023-12-01 18:16:43 24 4
gpt4 key购买 nike

以 ; 结尾的 java 类在语法上是否正确?在大多数文档中,java 类语法不以 ;

结尾

但是我成功地编译了下面的代码,

public class ClassDemo {
public static void main (String args[]) {
System.out.println ("Hello World!");
}
};

我的问题是,以 ; 结束类声明是否正确?为什么会被编译器忽略,而且最后有没有;似乎都无所谓?

最佳答案

您的代码在语法上是正确的,但 ; 是不必要的。

来自JLS 7.6.

Extra ";" tokens appearing at the level of type declarations in a compilation unit have no effect on the meaning of the compilation unit. Stray semicolons are permitted in the Java programming language solely as a concession to C++ programmers who are used to placing ";" after a class declaration. They should not be used in new Java code.

关于java - java类编写语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29076150/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com