It's not a secret anymore!
The moose likes Beginning Java and the fly likes Why does this code compile and run without errors ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does this code compile and run without errors ?" Watch "Why does this code compile and run without errors ?" New topic

Why does this code compile and run without errors ?

Saqib Mirza

Joined: Sep 26, 2007
Posts: 2

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

Because there's nothing illegal about it. Sloppy, yes, Illegal, no.

[Asking smart questions] [About Bear] [Books by Bear]
Stevens Miller

Joined: Jul 26, 2012
Posts: 853

Ah, yes. The mysterious semi-colon, so indispensable to our code, yet so trivial it is not even found in the index entries of books like "Core Java" or "Java: The Complete Reference." It's one of those things you just find yourself typing as you write your program, rarely even thinking about it. Trying to define it is kind of like trying to explain "red" to a blind person. You know what it is, but you can't quite put it into words.

For more semi-colon fun and games, try this snippet:

You get this output:

How can that be? Well, the single thing to remember (but only so long as you are thinking about this issue at all, after which, forget it for the rest of your days) is that a valid statement in Java must typically be followed by a semi-colon, and (here it comes) a valid statement in Java can be a zero-length string (or, if you prefer, any amount of whitespace characters). Thus, the two semi-colons that precede the "f" in line 1, in the code snippet, both follow valid Java statements. The first is a few spaces, which compiles to nothing and, of course, at run-time does nothing. The second follows a zero-length string, again compiling to, and doing, nothing. The two inside the parentheses are part of the syntax of a "for" statement. A complete "for" statement includes a valid statement following the rightmost ")" character. Here, that valid statement is, again, a zero-length string (followed by yet another, which is not part of the "for"statement; it just comes next in the source code on line 1).

Now, line 3 is a little more interesting. Like the "for" statement, an "if" statement must be followed by a valid statement. It looks like that statement is the one on line 4, but it isn't. The statement necessary to completing the "if" statement is another zero-length string, followed by a semi-colon (the one that ends line 3). This leads to the apparently bizarre case where an "if" statement tests for an always-false condition, yet seems to run the statement that depends on that always-false condition. It might be easier to understand if we reformat lines 3 and 4 a little bit:

This is exactly the same bit of code, but laid out differently. Now, maybe, you can see that the statement that "runs" if the "if" test passes is the zero-length statement (or whitespace statement, if you prefer to look at it that way) between the ")" at the end of the line "if (2 == 3)" and the semi-colon on the line below it. This can lead to some odd coding practices that you do see, from time to time, like this little nightmare:

There are only about a hundred-billion better ways to write this (at my last count), but (and I swear I am not making this up) you do see monstrosities like the above in other people's code.

Just never your own, I beg you.

In the other disciplines, we rise by standing on each others' shoulders. In computer science, we do it by standing on each others' toes.
Junilu Lacar

Joined: Feb 26, 2001
Posts: 6529

The standalone semi-colon is technically an empty statement, it does nothing but it still qualifies as a statement. Just look at the BNF and you'll see that at the bottom of the graphic.

Junilu - [How to Ask Questions] [How to Answer Questions]
I agree. Here's the link:
subject: Why does this code compile and run without errors ?
It's not a secret anymore!