Page 96 - Introduction to Programming with Java: A Problem Solving Approach
P. 96

                62
Chapter 3 Java Basics
public class Dream
{
}
public static void main(String[] args)
{
}
System.out.println("I have a dream!");
// end class Dream
The first brace is positioned immediately below the first character in the class heading, and the second brace is positioned immediately below the first character in the main heading. For readability’s sake, you should put a closing brace on a line by itself in the same column as its partner opening brace. Look at the above code fragment and note how the closing braces are positioned correctly.
3.7 System.out.println
In the Dream program, the main method contains this one statement: System.out.println("I have a dream!");
The System.out.println statement tells the computer to print something. The word System refers to
the computer. System.out refers to the output part of the computer system—the computer’s monitor. The
word println (pronounced “print line”) refers to the Java println method that’s in charge of printing a
message to the computer screen. The above statement would normally be referred to as a println method Apago PDF Enhancer
call. You call a method when you want to execute it.
The parentheses after println contain the message that is to be printed. The above statement prints
this message on a computer screen:
I have a dream!
 Note the double quotes in System.out.println("I
ters (e.g., I, space, h, a, v, e, . . .), you need to group them together. As you learned in Chapter 2, the double quotes are in charge of grouping together characters to form a string literal.
Note the semicolon at the end of System.out.println("I
the Java language is like a period in natural language. It indicates the end of a statement. You’ll need to put a semicolon at the end of every System.out.println statement.
You’ll be calling the System.out.println method a lot, so you might want to try to memorize its wording. To help with your memorization, think of it as an acronym—“Sop” for System, out, and println. Don’t forget that the S is uppercase and the rest of the command is lowercase.
The System.out.println method prints a message and then moves to the beginning of the next line. That means that if there is another System.out.println method call, it starts its printing on the next line. The upcoming example illustrates what we’re talking about.
An Example
In our Dream program, we print just one short line—“I have a dream!” In our next example, we print mul- tiple lines of varying lengths. See Figure 3.2’s Sayings program and its associated output. Note how each of the three println method calls produces a separate line of output. Note how the second println method call is too long to fit on one line, so we split it just to the right of the left parenthesis. The third println
have a
dream!"); To print a group of charac-
have a
dream!"); A semicolon in
   

































































   94   95   96   97   98