Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^74) | Java Syntax and Semantics, Classes, and Objects
In later chapters, we will define the syntax of a heading in greater detail. In the case of the
mainmethod, Heading is simply
public static void main(String[] args)
and may include a throwsclause, such as
throwsIOException
if the body contains exception-throwing method calls.
Here is the syntax template for a statement, limited to the Java statements discussed in
this chapter:
A statement can be empty (the null statement). The null statement is just a semicolon (;) and
looks like this:
;
It does absolutely nothing at execution time; execution just proceeds to the next statement.
It is rarely used.
As the syntax template shows, a statement also can be a field declaration, an executable
statement, or even a block. In the latter case, we see that we can use an entire block wher-
ever a single statement is allowed. In later chapters, when we introduce the syntax for
branching and looping structures, this fact will prove very important.
We use blocks often, especially as parts of other statements. Leaving out a {}pair can dra-
matically change the meaning as well as the execution of an application. That explains why
we always indent the statements inside a block—the indentation makes a block easy to spot
in a long, complicated section of code.
Notice that the syntax templates for the block and the statement do not mention semi-
colons. Yet the PrintNameapplication contains many semicolons. If you look back at the tem-
plates for a field declaration, assignment statement, and method call, you can see that a
semicolon is required at the end of each kind of statement. The syntax template for a block,
however, shows no semicolon after the right brace. The rule for using semicolons in Java, then,
is quite simple: Terminate each statement excepta block with a semicolon.
Statement
Null-Statement
Local-Field-Declaration
Assignment-Statement
Method-Call
Block

Free download pdf