Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Effect of semi-colon

 
Nikhil Pancholi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Just confused how semi-colon affects code compilation. For example the following:

interface I {

public class Inner {

Inner ( ) {
System .out . println ( "Inner Created" ) ;
}
};
};

compiles fine. I thought that because of two semi-colons after last two curly braces it won't. Why?

NIKHIL
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A semicolon is a statement terminator. A lone semicolon is an empty statement. These are valid pretty much anywhere, including after blocks, as you've used them here. They're just ignored.

Originally, this was allowed to make the transition from C++ easier -- or so the legend goes.
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the compiler sees


It interprets the close-curly as the end of the preceding block, just as if the semicolon weren't there. Then it interprets the semicolon as an empty statement, which is terminated by a semicolon just like non-empty statements.

Here's an extreme example. A source file could look like this:



The compiler consider this to be ten empty statements. Not a very useful source file, except that it teaches us something about semicolons. Hmmm, so maybe it is useful after all.
 
Nikhil Pancholi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it indeed was useful . Thanks for clearing my doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic