Java Reference
In-Depth Information
in the order in which the compiler lists them. As you become more experienced with
Java, you will learn how to spot and fix syntax errors quickly. Note that compilers not
only discover syntax errors, but also provide hints and sometimes tell the user where the
syntax errors are and how to fix them.
2
USE OF BLANKS
In Java, you use one or more blanks to separate numbers when data is input. Blanks are
also used to separate reserved words and identifiers from each other and from other
symbols. Blanks must never appear within a reserved word or identifier.
USE OF SEMICOLONS, BRACES, AND COMMAS
In Java, a semicolon is used to terminate a statement. The semicolon is also called a
statement terminator.
Note that braces, { and } , are not Java statements, even though they often appear on a
line with no other code. You might regard braces as delimiters because they enclose the
body of a method and set it off from other parts of the program. (Braces have other uses,
which will be explained in Chapter 4.)
Recall that commas are used to separate items in a list. For example, you use commas
when you declare more than one variable following a data type.
SEMANTICS
The set of rules that gives meaning to a language is called semantics. For example, the
order-of-precedence rules for arithmetic operators are semantic rules.
If a program contains syntax errors, the compiler will warn you. What happens when a
program contains semantic errors? It is quite possible to eradicate all syntax errors in a
program and still not have it run. And if it runs, it may not do what you meant it to do.
For example, the following two expressions are both syntactically correct expressions, but
they have different meanings:
2 + 3 * 5
and:
(2 + 3) * 5
If you substitute one of these expressions for the other in a program, you will not get the
same results—even though the numbers are the same, the semantics are different. You
will learn about semantics throughout this topic.
PROMPT LINES
Part of good documentation is the use of clearly written prompts so that users will know
what to do when they interact with a program. It is frustrating for a user to sit in front of a
running program and not have the foggiest notion of whether to enter something, and if
so, what to enter. Prompt lines are executable statements that inform the user what to
do. Consider the following Java statements, in which num is an int variable:
Search WWH ::




Custom Search