• Post Reply Bookmark Topic Watch Topic
  • New Topic

Mysterious but required semi colon, but why?  RSS feed

 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

I am just starting out on the SCJP book, after getting a fairly solid grounding in java, well at least syntactically, but just as I think I know something, another unobvious issue pops up.

As you can see from this code, the ; alone on its separate line, looks completely surplus to requirements. But removing it gives a compilation error. I often though for structures, typically have curly brackets, but when adding these in, the results again are different.

So, the mantra of this beginners forum is "no question to stupid", so can someone explain what is its purpose (I know it ensures only 1 value is printed from the enum), but is it part of the for, is it part of the for body? Hmmmmm

 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it, a bit of googling and all becomes clear.

The colon is part of the for loop, and ensures the lines thereafter only run once, so the for runs for each entry in the enum, but it is basically redundant code, and serves no purpose, and when removed you will still gets only WEDS printed.

This SCJP revision is more about trickery, but I suppose any good java dev should know this inside out. I'll try it on a few team members tomorrow and see how many do!

Just for any future posters of the same question, here is a summary of the article that helped explain it to me:

Occasionally, stray empty statements can cause annoying, hard-to-find errors in a program. For example, the following program segment prints out "Hello" just once, not ten times:



Why? Because the ";" at the end of the first line is a statement, and it is this statement that is executed ten times. The System.out.println statement is not really inside the for statement at all, so it is executed just once, after the for loop has completed.

 
Paul Mrozik
Ranch Hand
Posts: 117
Chrome Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Synes wrote:(I know it ensures only 1 value is printed from the enum

Hi Ben,

Actually, it doesn't do anything at all. The colon is just there to inform the compiler that the for loop scope ends right there.

If you do this:



It won't print anything.

To get the output you want, you can do this:



or this



In the second one, you have a chance to add more code, whereas in the first one the loop will only have a single line of code to work with.


 
Ben Synes
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, i figured that out. its a little snitchy trick to try and get newbies to fail perhaps the 1 question that gets them over the pass line. Its still good to know these gotchas, so thanks for your reply.

 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A tricky question indeed and easy to trip up on Ben.

A little tip...never use the one line conditional syntax. It is unclear, error prone and devs maintaining your code won't thank you for it.

The more curly braces the better I say!
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A for statement consists of the head part and of a following body statement.
The body can be a simple statement ending on ; including even an empty statement ending with a ;
or a compound statement: no, one or more statements ending with ;


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!