Java Reference
In-Depth Information
1.44
Reformat the following program according to the programming style and documenta-
tion guidelines. Use the end-of-line brace style.
✓
✓
Check
Point
public class
Test
{
// Main method
public static void
main(String[] args) {
/** Display output */
System.out.println(
"Welcome to Java"
);
}
}
Programming errors can be categorized into three types:
syntax errors
,
runtime errors
,
and
logic errors
.
Key
Point
1.11.1 Syntax Errors
Errors that are detected by the compiler are called
syntax errors
or
compile errors.
Syntax
errors result from errors in code construction, such as mistyping a keyword, omitting some
necessary punctuation, or using an opening brace without a corresponding closing brace.
These errors are usually easy to detect, because the compiler tells you where they are and
what caused them. For example, the program in Listing 1.5 has a syntax error, as shown in
Figure 1.19.
syntax errors
compile errors
L
ISTING
1.5
ShowSyntaxErrors.java
1
public class
ShowSyntaxErrors {
2
public static
main(String[] args) {
3 System.out.println(
"Welcome to Java
);
4 }
5 }
Four errors are reported, but the program actually has two errors:
The keyword
void
is missing before
main
in line 2.
■
The string
Welcome to Java
should be closed with a closing quotation mark in line 3.
■
Since a single error will often display many lines of compile errors, it is a good practice to
fix errors from the top line and work downward. Fixing errors that occur earlier in the pro-
gram may also fix additional errors that occur later.
Compile
F
IGURE
1.19
The compiler reports syntax errors.