• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Effect of semi-colon

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it indeed was useful . Thanks for clearing my doubt.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic