Modifying Our First Java Program

This section continues our introduction to Java programming with two examples that 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 to produce the same output as that shown in Fig. 2.1. From this point forward, we highlight the new and key features in each code listing, as shown in lines 910 of this program.

Figure 2.3. Printing a line of text with multiple statements.

(This item is displayed on pages 43 - 44 in the print version)

1 // Fig. 2.3: Welcome2.java 2 // Printing a line of text with multiple statements. 3 4 public class Welcome2 5 { 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.print( "Welcome to " ); 10 System.out.println( "Java Programming!" ); 11 12 } // end method main 13 14 } // end class Welcome2  

Welcome to Java Programming!  

The program is almost identical to Fig. 2.1, so we discuss only the changes here. Line 2

// Printing a line of text with multiple statements.

is an end-of-line comment stating the purpose of this program. Line 4 begins the Welcome2 class declaration.

Lines 910 of method main

System.out.print( "Welcome to" ); System.out.println( "Java Programming!" );

display one line of text in the command window. The first statement uses System.out's method print to display a string. Unlike println, after displaying its argument, print does not position the output cursor at the beginning of the next line in the command windowthe 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). Each print or println statement resumes displaying characters from where the last print or println statement stopped displaying characters.

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 they should 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. Figure 2.4 outputs four lines of text, using newline characters to determine when to begin each new line.

Figure 2.4. Printing multiple lines of text with a single statement.

(This item is displayed on page 45 in the print version)

1 // Fig. 2.4: Welcome3.java 2 // Printing multiple lines of text with a single statement. 3 4 public class Welcome3 5 { 6 // main method begins execution of Java application 7 public static void main( String args[] ) 8 { 9 System.out.println( "Welcome to Java Programming!" ); 10 11 } // end method main 12 13 } // end class Welcome3  

Welcome to Java Programming!  

Most of the program is identical to those in Fig. 2.1 and Fig. 2.3, so we discuss only the changes here. Line 2

// Printing multiple lines of text with a single statement.

is a comment stating the purpose of this program. Line 4 begins the Welcome3 class declaration.

Line 9

System.out.println( "Welcome to Java Programming!" );

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 two characters and n (repeated three times in the statement) do not appear on the screen. The backslash() is called an escape character. It indicates to System.out's print and println methods that a "special character" is to be output. When a backslash appears in a string of characters, Java combines the next character with the backslash to form an escape sequence. The escape sequence represents the newline character. When a new-line 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.

Figure 2.5. Some common escape sequences.

Escape sequence

Description

Newline. Position the screen cursor at the beginning of the next line.

Horizontal tab. Move the screen cursor to the next tab stop.

Carriage return. Position the screen cursor at the beginning of the current linedo 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( ""in quotes"" );  

 

displays

"in quotes"  

Категории