Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the semicolon not required but allowed at the end of a class definition?

Tags:

java

syntax

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

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

That is, in C++:

class Person {
    public:
    string name;
    int number;
}; // Note this semicolon

But in 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:

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?

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.

Well, I've already seen Semicolons in a class definition and other related questions.

Thanks in advance.

like image 350
Vedant Terkar Avatar asked Jul 07 '14 15:07

Vedant Terkar


People also ask

Why use a semicolon at the end of a class?

You have to put the semicolon there so the compiler will know whether you declared any instances or not. This is a C compatibility thing. Doesn't even need the name or constituents: class { } waldo; is legal.

Why do we use semicolons after classes in C++?

Semicolon is often used to delimit one bit of C++ source code, indicating it's intentionally separated from the respective code. Usage of Semicolon in C++ is after class and structure definitions, variable declarations, function declarations, after each statement generally.

Why does every line of code end with a semicolon?

The semi-colon, ; , in JavaScript represents an “end of statement” or “statement delimiter”. It tells JavaScript not to parse any more code, and to go ahead and execute the statement up to that point.

What happens if you put a semicolon at the end of an if statement?

Do not use a semicolon on the same line as an if , for , or while statement because it typically indicates programmer error and can result in unexpected behavior.


3 Answers

I've compiled and executed both the program snippets shown for Java, and they both work. Can anyone explain why this is so?

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

What does the semicolon at the end of a class definition in Java stand for?

Nothing. It is optional "syntactic noise".

The JLS explains it as follows:

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.


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

like image 135
Stephen C Avatar answered Oct 07 '22 16:10

Stephen C


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.

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:

CompilationUnit:
 PackageDeclaration(opt) ImportDeclarations(opt) TypeDeclarations(opt)

And a type declaration is any one of the following, according to 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:

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:

EmptyStatement:
    ;
like image 20
Andy Thomas Avatar answered Oct 07 '22 16:10

Andy Thomas


Java added this just for persons like you who switch from 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.

For example, also the following is correct in 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:

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.

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(){}    

}
like image 35
gexicide Avatar answered Oct 07 '22 16:10

gexicide