This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Why does this code compile and run without errors ?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because there's nothing illegal about it. Sloppy, yes, Illegal, no.
 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 17293
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I can't beleive you just said that. Now I need to calm down with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic