- 2.1 Introduction
- 2.2 Your First Program in Java: Printing a Line of Text
- 2.3 Modifying Your First Java Program
- 2.4 Displaying Text with printf
- 2.5 Another Application: Adding Integers
- 2.6 Memory Concepts
- 2.7 Arithmetic
- 2.8 Decision Making: Equality and Relational Operators
- 2.9 Wrap-Up
- Summary
- Self-Review Exercises
- Answers to Self-Review Exercises
- Exercises
- Making a Difference
2.3 Modifying Your First Java Program
In this section, we modify the example in Fig. 2.1 to print text on one line by using multiple statements and to print text on several lines by using a single statement.
Displaying a Single Line of Text with Multiple Statements
Welcome to Java Programming! can be displayed several ways. Class Welcome2, shown in Fig. 2.3, uses two statements (lines 9–10) to produce the output shown in Fig. 2.1. [Note: From this point forward, we highlight the new and key features in each code listing, as we've done for lines 9–10.]
Fig 2.3. Printing a line of text with multiple statements.
1// Fig. 2.3: Welcome2.java
2// Printing a line of text with multiple statements.
3 4public class
Welcome2 5 { 6// main method begins execution of Java application
7public static void
main( String[] args ) 8 { 9System.out.print(
"Welcome to " );
10System.out.println( "Java Programming!" );
11 }// end method main
12 }// end class Welcome2
The program is similar to Fig. 2.1, so we discuss only the changes here. Line 2
|
is an end-of-line comment stating the purpose of the program. Line 4 begins the Welcome2 class declaration. Lines 9–10 of method main
System.out.print( |
display one line of text. The first statement uses System.out's method print to display a string. Each print or println statement resumes displaying characters from where the last print or println statement stopped displaying characters. Unlike println, after displaying its argument, print does not position the output cursor at the beginning of the next line in the command window—the next character the program displays will appear immediately after the last character that print displays. Thus, line 10 positions the first character in its argument (the letter "J") immediately after the last character that line 9 displays (the space character before the string's closing double-quote character).
Displaying Multiple Lines of Text with a Single Statement
A single statement can display multiple lines by using newline characters, which indicate to System.out's print and println methods when to position the output cursor at the beginning of the next line in the command window. Like blank lines, space characters and tab characters, newline characters are white-space characters. The program in Fig. 2.4 outputs four lines of text, using newline characters to determine when to begin each new line. Most of the program is identical to those in Fig. 2.1 and Fig. 2.3.
Fig 2.4. Printing multiple lines of text with a single statement.
1// Fig. 2.4: Welcome3.java
2// Printing multiple lines of text with a single statement.
3 4public class
Welcome3 5 { 6// main method begins execution of Java application
7public static void
main( String[] args ) 8 { 9 System.out.println("Welcome\n to \nJava\nProgramming!" );
10 }// end method main
11 }// end class Welcome3
Line 2
|
is a comment stating the program's purpose. Line 4 begins the Welcome3 class declaration.
Line 9
System.out.println(
|
displays four separate lines of text in the command window. Normally, the characters in a string are displayed exactly as they appear in the double quotes. Note, however, that the paired characters \ and n (repeated three times in the statement) do not appear on the screen. The backslash ( \ ) is an escape character. which has special meaning to System.out's print and println methods. When a backslash appears in a string, Java combines it with the next character to form an escape sequence. The escape sequence \n represents the newline character. When a newline character appears in a string being output with System.out, the newline character causes the screen's output cursor to move to the beginning of the next line in the command window.
Figure 2.5 lists several common escape sequences and describes how they affect the display of characters in the command window. For the complete list of escape sequences, visit java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.6.
Fig 2.5. Some common escape sequences.
Escape sequence |
Description |
\n |
Newline. Position the screen cursor at the beginning of the next line. |
\t |
Horizontal tab. Move the screen cursor to the next tab stop. |
\r |
Carriage return. Position the screen cursor at the beginning of the current line—do not advance to the next line. Any characters output after the carriage return overwrite the characters previously output on that line. |
\\ |
Backslash. Used to print a backslash character. |
\" |
Double quote. Used to print a double-quote character. For example, System.out.println(
|
displays "in quotes". |