Section1.7From the Java Library: System and PrintStream
Java comes with a library of classes, organized into packages, that are used to perform common tasks. In this and similar sections in subsequent chapters, we will feature some of the most useful built-in classes. In this section we introduce the System and PrintStream classes, which are used for printing a program’s output.
Java programs need to be able to accept input and to display output. Deciding how a program will handle input and output (I/O) is part of designing its user interface, a topic we take up in detail in Chapter 4.
The simplest type of user interface is a command-line interface, in which input is taken from the command line through the keyboard, and output is displayed on the console. Some Java applications, like HelloWorld (Listing 1.5.1), use this type of interface.
In contrast, a Graphical User Interface (GUI), uses buttons, text fields, canvases, and other graphical components for input and output. The HelloWorldCanvas program (Listing 1.5.16), is an example of a GUI.
This section describes how Java handles simple console output.
In Java, any source or destination for I/O is considered a stream of bytes or characters. To perform output, we insert bytes or characters into the stream. To perform input, we extract bytes or characters from the stream. Even characters entered at a keyboard, if considered as a sequence of keystrokes, can be represented as a stream.
There are no I/O commands or keywords in the Java language. Instead, I/O is handled through methods that belong to classes contained in the java.io package. We have already seen how the output method println() is used to output a string to the console. For example, the following println() statement
prints the message “Hello, World” on the Java console. Let’s now examine this statement more carefully to see how it makes use of the Java I/O classes.
The java.io.PrintStream class is Java’s printing expert, so to speak. It contains a variety of print() and println() methods that can be used to print all of the various types of data we find in a Java program. A partial definition of PrintStream is shown in Figure 1.7.1. Note that in this case the PrintStream class has no attributes, just methods.
Because the various print() and println() methods are instance methods of a PrintStream object, we can only use them by finding a PrintStream object and “telling” it to print data for us. As shown in Figure 1.7.2, Java’s java.lang.System class contains two public (+) PrintStream objects, System.out and System.err.
As its name suggests, the System.err stream is used primarily for error messages, whereas the System.out stream is used for other printed output. Similarly, as its name suggests, the System.in object can be used to handle input, which will be covered in Chapter 2.
The only difference between the print() and println() methods is that println() will also print a carriage return and line feed after printing its data, thereby allowing subsequent output to be printed on a new line. For example, the following statements
Now that we know how to use Java’s printing expert, let’s use it to “sing” a version of “Old MacDonald Had a Farm.” As you might guess, this program will simply consist of a sequence of System.out.println() statements each of which prints a line of the verse. The complete program is shown in Listing 1.7.3. You can run the program in the self-study exercises below.
This example illustrates the usefulness of the Java class library. If there’s a particular task we want to perform, one of the first things we should ask is whether there is already an “expert” in Java’s class library that performs that task. If so, we can use methods provided by the expert to perform that particular task.
Principle1.7.4.EFFECTIVE DESIGN: Using the Java Library.
Learning how to use classes and objects from the Java class library is an important part of object-oriented programming in Java.
1.Old MacDonald Song.
Run the Program below. Modify the OldMacDonald class to “sing” one more verse of the song for an animal of your choice.
2.Happy Face Pattern.
Complete and run the program below to print out the pattern: