5

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

pacman
  • 1,061
  • 1
  • 17
  • 36
  • 2
    Did you consult the Language Specification? If not, why not? – Ingo Sep 27 '11 at 14:43
  • 1
    I did check out the following link for language specification, but my cursory search didn't produce anything. Do you have a link that I can look? http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html – pacman Sep 27 '11 at 18:21
  • 1
    http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.6 – Ingo Sep 27 '11 at 20:02

7 Answers7

11

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

stivlo
  • 83,644
  • 31
  • 142
  • 199
  • 1
    Specifically, the emptiness between the two semicolons is an empty statement. – jwodder Sep 27 '11 at 14:42
  • 3
    And it is to be avoided, because the compiler inserts 10000 NOPs in the bytecode for every empty statement .... no, just kidding :) – Ingo Sep 27 '11 at 14:44
  • 1
    @stivlo. Thanks for the answer. I tested using the Eclipse Indigo and Helios version and both didn't complain. – pacman Sep 27 '11 at 18:24
  • @pacman, see my updated answer. BTW I'm using Indigo, but this isn't a new feature. – stivlo Sep 28 '11 at 02:08
  • @stivlo. Looks like in my eclipse's preference. Empty statement was set to ignore for some reason. Now I see the warning as well. I updated my question with screenshots. Thanks for your time though. – pacman Sep 28 '11 at 14:28
2

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());;;;;;;;;;;;;;;
nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
2

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.

wormsparty
  • 2,481
  • 19
  • 31
  • It's good to see why a statement `;;;;` is valid in Java. Perhaps we'll never know with 100% certainty. Now the question becomes though: will this ever change and become invalid? – fIwJlxSzApHEZIl Jan 15 '18 at 18:32
  • I don't think it will ever change - and most people use the same JavaC compiler from Oracle. Even if it does ever, it's a very simple thing to update, just call a regex on all your files like s/;{2,}/;/g – wormsparty Jan 16 '18 at 18:49
2

; 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");;;;;;;;
thirtydot
  • 224,678
  • 48
  • 389
  • 349
subodh
  • 6,136
  • 12
  • 51
  • 73
1

; means end of statement.

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

TheOneTeam
  • 25,806
  • 45
  • 116
  • 158
1

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

int x = 0;
;

No problems there!

MGZero
  • 5,812
  • 5
  • 29
  • 46
0

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.

Santosh
  • 17,667
  • 4
  • 54
  • 79