pacman
pacman

Reputation: 1061

Compiler doesn't complain when I ended a line with two semicolons. Why?

I thought bad thing would happen when I ended a line like this. But compiler didn't even complain. Does anybody have an idea, why this is legal in java.

displayDataMap.put("dateInterval", getDateInterval());;

Edit: The reason Eclipse wasn't complaining was because in preference->java->compiler->Errors/Warning I had the Empty statement: as ignore.

enter image description here

Upvotes: 5

Views: 4194

Answers (7)

stivlo
stivlo

Reputation: 85516

It's an empty statement and is valid. As pointed out by jwodder, to be more precise the empty statement is the lack of statement between the two semicolon and could be represented by two consecutive semicolons or by semicolons separated by white space (that is including new lines).

IDE like Eclipse will warn you about this. If you check the picture below, I've inserted a double semicolon on line 236. The red lines were added by me to focus the attention, but Eclipse give many visual cues:

  • a jagged yellow line under the semicolons.
  • a light bulb with a warning sign on the left margin of line 236, by hovering on that line, it will popup a small message saying "Unnecessary semicolon".
  • on the bar containing the directories and packages, the warning sign is shown again. It is shown for every package or source folder, which contains at least one warning.
  • on the package explorer and on the navigator file icon the same light bulb icon will be shown (not in this screenshot).

enter image description here

Upvotes: 11

subodh
subodh

Reputation: 6158

; represents the empty statement or you can say, it's represent termination line of a any statement in Java, even if you put more than one it's valid in Java. Compiler will not gives you any error because this is valid. even the below statement is also valid:

System.out.println("HI");;;;;;;;

Upvotes: 2

wormsparty
wormsparty

Reputation: 2499

Here's a practical example in C:

#ifndef NDEBUG
#define Dprintf printf
#else
#define Dprintf(X)
#endif

if(some_test)
   Dprintf("It's true!");
else
   Dprintf("It's false.");

If NDEBUG is defined, the compiler will see:

if(some_test)
   ;
else
   ;

This can be useful in some cases.

Since Java is heavily inspired by C/C++, it's something they kept, even though it is not useful and there is no preprocessor.

You can, if you want, run the C preprocessor cpp on your code to do this kind of behaviour.

Some languages may say it's an error instead, it's up to them to choose.

Upvotes: 2

MGZero
MGZero

Reputation: 5963

It's an empty statement, it's the same as if you did this:

int x = 0;
;

No problems there!

Upvotes: 1

Kit Ho
Kit Ho

Reputation: 26998

; means end of statement.

for ;; , the second ; will imply that the statement consists of nothing.

Upvotes: 1

Santosh
Santosh

Reputation: 17923

Compiler wont complain even when you just create an empty file with .java extension. Its an empty compilation unit and its valid. Same way a semicolon is an empty statement which is valid.

Upvotes: 0

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

The compiler uses semicolon to denote an 'end of statement'. Having two consecutively ends the statement prior to the first, and then creates a second empty statement.

Really, you could even do this:

displayDataMap.put("dateInterval", getDateInterval());;;;;;;;;;;;;;;

Upvotes: 2

Related Questions