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"
 


Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net