Java Reference
In-Depth Information
Good Programming Practice 2.5
Indent the entire body of each method declaration one “level” between the braces that de-
fine the body of the method. This makes the structure of the method stand out and makes
the method declaration easier to read.
Performing Output with
System.out.println
Line 9
System.out.println(
"Welcome to Java Programming!"
);
instructs the computer to perform an action—namely, to display the characters contained
between the double quotation marks (the quotation marks themselves are
not
displayed).
Together, the quotation marks and the characters between them are a
string
—also known
as a
character string
or a
string literal
. White-space characters in strings are
not
ignored
by the compiler. Strings
cannot
span multiple lines of code.
The
System.out
object—which is predefined for you—is known as the
standard
output object
. It allows a Java application to display information in the
command
window
from which it executes. In recent versions of Microsoft Windows, the command
window is the
Command Prompt
. In UNIX/Linux/Mac OS X, the command window is
called a
terminal window
or a
shell
. Many programmers call it simply the
command line
.
Method
System.out.println
displays (or prints) a line of text in the command
window. The string in the parentheses in line 9 is the
argument
to the method. When
System.out.println
completes its task, it positions the output cursor (the location where
the next character will be displayed) at the beginning of the next line in the command
window. This is similar to what happens when you press the
Enter
key while typing in a
text editor—the cursor appears at the beginning of the next line in the document.
The entire line 9, including
System.out.println
, the argument
"Welcome
to
Java
Programming!"
in the parentheses and the
semicolon
(
;
), is called a
statement
. A method
typically contains one or more statements that perform its task. Most statements end with
a semicolon. When the statement in line 9 executes, it displays
Welcome
to
Java
Program-
ming!
in the command window.
When learning how to program, sometimes it's helpful to “break” a working program
so you can familiarize yourself with the compiler's syntax-error messages. These messages do
not always state the exact problem in the code. When you encounter an error, it will give you
an idea of what caused it. [Try removing a semicolon or brace from the program of Fig. 2.1,
then recompile the program to see the error messages generated by the omission.]
Error-Prevention Tip 2.3
When the compiler reports a syntax error, it may not be on the line that the error message
indicates. First, check the line for which the error was reported. If you don't find an error
on that line, check several preceding lines.
Using End-of-Line Comments on Right Braces for Readability
As an aid to programming novices, we include an end-of-line comment after a closing
brace that ends a method declaration and after a closing brace that ends a class declaration.
For example, line 10
}
// end method main