File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Effect of semi-colon Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Effect of semi-colon " Watch "Effect of semi-colon " New topic
Author

Effect of semi-colon

Nikhil Pancholi
Greenhorn

Joined: Dec 10, 2004
Posts: 7
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
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.


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Nikhil Pancholi
Greenhorn

Joined: Dec 10, 2004
Posts: 7
Yes it indeed was useful . Thanks for clearing my doubt.
 
Consider Paul's rocket mass heater.
 
subject: Effect of semi-colon
 
Similar Threads
Generics <?>
how to execute complex queries
A Simple Control Flow Question
Anonymous Class
decode an inputstream