Java 为什么在类定义的末尾不需要分号但允许使用分号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24614393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is the semicolon not required but allowed at the end of a class definition?
提问by Vedant Terkar
I'm trying to shift from C++ to Java.
我正在尝试从 C++ 转向 Java。
What I wonder is, in C++, after a class definition, a semicolon (;
) is required, but in Java it isn't.
我想知道的是,在 C++ 中,在类定义之后,需要一个分号 ( ;
),而在 Java 中则不需要。
That is, in C++:
也就是说,在 C++ 中:
class Person {
public:
string name;
int number;
}; // Note this semicolon
But in Java:
但是在 Java 中:
class Person {
public String name;
public int number;
} // Semicolon is not required
That's fine, I understand that.
没关系,我明白了。
However, my problem is:
但是,我的问题是:
Java also works when I add semicolon at end of class definition, like:
当我在类定义的末尾添加分号时,Java 也可以工作,例如:
class Person {
public String name;
public int number;
}; // Now this semicolon is confusing me!
I've compiled and executed both the program snippets shown for Java, and they both work. Can anyone explain why this is so? What does the semicolon at the end of a class definition in Java stand for?
我已经编译并执行了为 Java 显示的两个程序片段,它们都可以工作。谁能解释为什么会这样?Java中类定义末尾的分号代表什么?
I'm sorry if this question is of low quality, but I really need clarification for this. I hope experts in Java will help me.
如果这个问题质量低,我很抱歉,但我真的需要对此进行澄清。希望Java高手帮帮我。
Well, I've already seen Semicolons in a class definitionand other related questions.
Thanks in advance.
提前致谢。
采纳答案by Stephen C
I've compiled and executed both the program snippets shown for Java, and they both work. Can anyone explain why this is so?
我已经编译并执行了为 Java 显示的两个程序片段,它们都可以工作。谁能解释为什么会这样?
It is allowed because the Java Grammar says it is allowed; See JLS 7.6.
它是允许的,因为 Java 语法说它是允许的;请参阅JLS 7.6。
What does the semicolon at the end of a class definition in Java stand for?
Java中类定义末尾的分号代表什么?
Nothing. It is optional "syntactic noise".
没有。它是可选的“句法噪声”。
The JLS explains it as follows:
JLS 解释如下:
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 编程语言中允许使用杂散分号,这只是对习惯于放置“;”的 C++ 程序员的一种让步。在类声明之后。它们不应在新的 Java 代码中使用。
(Note that this is NOT an "empty statement". An empty statement (JLS 14.6) appears in a syntactic context where a statement is allowed. The presence of an empty statement can change the meaning of your code; e.g. if (a == b) ; c();
versus if (a == b) c();
)
(请注意,这不是“空语句”。空语句(JLS 14.6)出现在允许使用语句的句法上下文中。空语句的存在可以改变代码的含义;例如if (a == b) ; c();
vs if (a == b) c();
)
回答by gexicide
Java added this just for persons like you who switch from C++!
Java 只是为像你这样从 C++ 切换的人添加了这个!
In Java, a single semicolon is a declaration that may be written almost everywhere. Its only purpose is to ease the transition from such languages.
在 Java 中,单个分号是一个几乎可以写在任何地方的声明。它的唯一目的是简化从这些语言的过渡。
For example, also the following is correct in Java:
例如,以下在 Java 中也是正确的:
;;;;;;;;;;
class X{
;;;;;;;;;;;;;;
} ;;;;;;;;;;;;;;;
A semicolon is simply treated as an empty declaration that does nothing.
分号被简单地视为什么都不做的空声明。
Here is a quote from the spec, paragraph 7.6:
这是规范第 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 aconcession to C++ programmers who are used to placing ";" after aclass declaration.They should not be used in new Java code.
额外的 ”;” 出现在编译单元中类型声明级别的标记对编译单元的含义没有影响。Java 编程语言中允许使用杂散分号,这只是对习惯于放置“;”的 C++ 程序员的一种让步。在类声明之后。它们不应在新的 Java 代码中使用。
So as you see, this is really just for guys like you :).
所以如你所见,这真的只适合像你这样的人:)。
You can even use a line of semicolons as a nice visual separation. However, I strongly advise against this. But it might be good for bragging purposes.E.g.:
您甚至可以使用一行分号作为很好的视觉分隔。但是,我强烈建议不要这样做。但这可能有利于吹牛。例如:
class X {
// Member variables
private int i;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
// Constructors
X(){}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
// Methods
void foo(){}
}
回答by rwols
I'm not familiar with Java, but the reason why there's an extra semicolon needed is because you can define an anonymous class, inside functions. For instance:
我不熟悉 Java,但需要额外分号的原因是因为您可以在函数内部定义匿名类。例如:
void routine(const int x, const int y)
{
class { public: int x; int y; } my_instance;
my_instance.x = x;
my_instance.y = y;
// ... etc
}
You'll usually see this more with structs than with classes, to capture some important variables of a big class.
您通常会在结构中看到这一点,而不是在类中,以捕获大类的一些重要变量。
void f(const BigClass& big_class)
{
struct { std::string str; int i; } props;
props.str = big_class.GetFilename();
props.i = big_class.GetID();
// etc...
}
回答by Andy Thomas
In the early days, allowing this was one of the ways in which Java was made more familiar to C/C++ programmers. This familiarity was important to the adoption of Java.
在早期,允许这是 C/C++ 程序员更熟悉 Java 的方式之一。这种熟悉对于采用 Java 很重要。
Here's how it works syntactically.
这是它在语法上的工作方式。
After a top-level class,it works because a compilation unit contains this sequence, according to JLS 7.3:
根据JLS 7.3,在顶级类之后,它可以工作,因为编译单元包含此序列:
CompilationUnit:
PackageDeclaration(opt) ImportDeclarations(opt) TypeDeclarations(opt)
And a type declaration is any one of the following, according to JLS 7.6:
根据JLS 7.6,类型声明是以下任何一种:
TypeDeclaration:
ClassDeclaration
InterfaceDeclaration
;
After a member class,nested in some other class, it works because a semicolon can be a class member declaration, according to JLS 8.1.6:
根据JLS 8.1.6 ,在嵌套在其他类中的成员类之后,它可以工作,因为分号可以是类成员声明:
ClassMemberDeclaration:
FieldDeclaration
MethodDeclaration
ClassDeclaration
InterfaceDeclaration
;
After a local class,inside a method, it works because a semicolon can be an empty statement, according to JLS 14.6:
根据JLS 14.6 ,在本地类之后,在方法内部,它可以工作,因为分号可以是空语句:
EmptyStatement:
;
回答by Pythoner
I think this is a followed style which inherites from C:
我认为这是一种继承自 C 的遵循风格:
struct A
{
unsigned a;
unsigned b;
};
Or you can use:
或者你可以使用:
struct A
{
unsigned a;
unsigned b;
}A1,A2;