Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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

like image 430
pacman Avatar asked Sep 27 '11 14:09

pacman


4 Answers

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

like image 160
stivlo Avatar answered Oct 13 '22 20:10

stivlo


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());;;;;;;;;;;;;;;
like image 28
nicholas.hauschild Avatar answered Oct 13 '22 22:10

nicholas.hauschild


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.

like image 2
wormsparty Avatar answered Oct 13 '22 20:10

wormsparty


; 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");;;;;;;;
like image 2
subodh Avatar answered Oct 13 '22 21:10

subodh