为什么是分号;不需要,但允许在类定义的结尾? [英] Why is the semicolon ; not required but allowed at the end of a class definition?

查看:276
本文介绍了为什么是分号;不需要,但允许在类定义的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从C ++转向Java。

Well I'm trying to shift from C++ to Java.

我想知道的是,在C ++中,类定义之后是一个分号( )是必需的。

What I wonder is, In C++ after class definition a semicolon(;) is Required. But in java it isn't required.

也就是说,在C ++中:

That is, in C++:

class Person{
  public:
  string name;
  int number;
}; // Note This Semicolon

但在Java中:

class Person{
  public String name;
  public int number;
} // Semicolon Isn't Required

这很好我已经明白了。

That's Fine I've understood it.

但我的问题是

当我在分号类定义的结束,如:

Java also works when I add semicolon at end of class definition, like:

class Person{
 public String name;
 public int number;
}; // Now This Semicolon(;) Is confusing me!

我编译并执行了 java code>和他们都工作。谁能解释为什么会是这样?对于 java 的类定义中的分号是什么意思?

I've compiled and executed both the program snippets shown for java and they both work. can anyone explain why is it so? what does semicolon in class definition for java stands for?

对不起如果问题是质量低,需要澄清这一点。希望专家在java将帮助我。

Sorry If question is of low quality, But I really need clarification for this. Hope experts in java will help me.

我已经看到了:

类定义中的分类。和其他亲戚。

提前感谢。

推荐答案


我编译并执行了为Java显示的程序片段,谁能解释为什么会是这样?

I've compiled and executed both the program snippets shown for java and they both work. can anyone explain why is it so?

这是允许的,因为Java语法说它是允许的;请参见 JLS 7.6

It is allowed because the Java Grammar says it is allowed; See JLS 7.6.


java的类定义中的分号是什么意思?

what does semicolon in class definition for java stands for?

没有。这是可选的句法噪声。

Nothing. It is optional "syntactic noise".

JLS解释如下:


额外;出现在编译单元中的类型声明级别的标记对编译单元的含义没有影响。 Java编程语言中允许使用纯分号作为对C ++程序员的特许权,C ++程序员习惯于放置;后的类声明。它们不应在新的Java代码中使用。

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.






(请注意,这不是一个空语句空语句( JLS 14.6 )出现在语法中一个空语句的存在可以改变你的代码的含义;例如 if(a == b); c(); vs. if(a == b)c();


(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();)

这篇关于为什么是分号;不需要,但允许在类定义的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆