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.