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.
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:
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());;;;;;;;;;;;;;;
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.
;
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");;;;;;;;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With