What i wonder is, in C++, after a class definition, a semicolon (;) is required, but in Java it isn"t.

You are watching: All java statements end with semicolons.

That is, in C++:

class human public: wire name; int number;; // note this semicolonBut in Java:

class person public String name; public int number; // Semicolon is not requiredThat"s fine, I understand that.

However, my trouble is:

Java also works once I include semicolon at end of course definition, like:

class human public String name; windy int number;; // now this semicolon is confound me!I"ve compiled and executed both the routine snippets presented for Java, and they both work. Can anyone describe why this is so? What does the semicolon in ~ the end of a class meaning in Java stand for?

I"m i m really sorry if this inquiry is of short quality, but I really require clarification for this. Ns hope professionals in Java will help me.

Well, I"ve currently seen Semicolons in a class definition and various other related questions.

Thanks in advance.


java syntax
share
enhance this inquiry
monitor
edited jan 2 "18 at 11:22
*

Stephen C
650k9090 gold badges748748 silver- badges11261126 bronze title
inquiry Jul 7 "14 in ~ 15:35
*

Vedant TerkarVedant Terkar
4,18355 gold badges3232 silver- badges5858 bronze title
1
include a comment |

5 answer 5


active oldest Votes
30
I"ve compiled and executed both the routine snippets displayed for Java, and also they both work. Have the right to anyone explain why this is so?

It is permitted because the Java Grammar claims it is allowed; check out JLS 7.6.

What does the semicolon in ~ the finish of a class an interpretation in Java was standing for?

Nothing. It is optional "syntactic noise".

The JLS describes it together follows:

Extra ";" tokens appearing at the level of form declarations in a compilation unit have actually no result on the an interpretation of the compilation unit. Stray semicolons are permitted in the Java programming language solely as a concession to C++ programmers that are supplied to place ";" ~ a class declaration. They must not be offered in new Java code.

(Note the this is not an "empty statement". An north statement (JLS 14.6) shows up in a construction context whereby a statement is allowed. The existence of an north statement can readjust the definition of her code; e.g. If (a == b) ; c(); matches if (a == b) c();)


re-superstructure
improve this prize
follow
edited january 2 "18 at 10:55
*

MC Emperor
19k1313 gold badges7373 silver badges111111 bronze badges
reply Jul 7 "14 in ~ 15:49
*

Stephen CStephen C
650k9090 yellow badges748748 silver- badges11261126 bronze badges
1
include a comment |
11
In the early days, allowing this was one of the ways in i beg your pardon Java was made an ext familiar to C/C++ programmers. This familiarity was necessary to the adoption of Java.

Here"s just how it functions syntactically.

After a top-level class, that works since a compilation unit has this sequence, follow to JLS 7.3:

CompilationUnit: PackageDeclaration(opt) ImportDeclarations(opt) TypeDeclarations(opt)And a form declaration is any type of one that the following, follow to JLS 7.6:

TypeDeclaration: ClassDeclaration InterfaceDeclaration ;After a member class, nested in some other class, the works because a semicolon can be a class member declaration, according to JLS 8.1.6:

ClassMemberDeclaration: FieldDeclaration MethodDeclaration ClassDeclaration InterfaceDeclaration ;After a regional class, within a method, that works due to the fact that a semicolon can be an empty statement, according to JLS 14.6:

EmptyStatement: ;
re-publishing
improve this answer
follow
edited Jul 7 "14 in ~ 16:35
answer Jul 7 "14 at 15:46

*

Andy ThomasAndy thomas
80.5k1010 gold badges9595 silver- badges145145 bronze badges
1
include a comment |
9
Java added this simply for persons favor you that switch native C++!

In Java, a single semicolon is a declaration that might be written practically everywhere. Its only purpose is to lull the transition from together languages.

For example, also the following is exactly in Java:

;;;;;;;;;;class X ;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;A semicolon is just treated together an north declaration the does nothing.

Here is a quote native the spec, paragraph 7.6:

Extra ";" tokens appearing at the level of type declarations in a compilation unit have actually no effect on the an interpretation of the compilation unit. Stray semicolons are permitted in the Java programming language solely as a concession to C++ programmers that are supplied to put ";" after ~ a class declaration. They have to not be used in new Java code.

So as you see, this is really just for men like friend :).

See more: Telling Someone What They Want To Hear, What Is An English Word That Means

You can even use a line of semicolons together a nice intuitive separation. However, ns strongly advise versus this. However it can be an excellent for bragging purposes. E.g.:

class X // Member variables exclusive int i; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; // Constructors X() ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; // methods void foo()