Skip to main content

Section 1.7 From 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 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

System.out.println("Hello, World");

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 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.

Figure 1.7.1. A UML class diagram of the PrintStream class.

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.

Figure 1.7.2. A UML class diagram of the PrintStream class.

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 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

System.out.println("hello again");

would produce the following output:

hellohello again goodbye

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.

 public class OldMacDonald
    public static void main(String args[])   // Main method
System.out.println("Old MacDonald had a farm");
System.out.println("E I E I O.");
System.out.println("And on his farm he had a duck.");
System.out.println("E I E I O.");
System.out.println("With a quack quack here.");
System.out.println("And a quack quack there.");
System.out.println("Here a quack, there a quack,");
System.out.println("Everywhere a quack quack.");
System.out.println("Old MacDonald had a farm");
System.out.println("E I E I O.");
    }  // End of main
 }  // End of OldMacDonald
Listing 1.7.3. The class.

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.

Exercises Self-Study Exercises

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:

* **  ** * 
*   **   *
* *    * * 
*  ****  * 

You have attempted of activities on this page.