1.6. From the Java Library: System and PrintStreamJAVA COMES with a library of classes that can be used to perform common tasks. The Java class library is organized into a set of packages, where each package contains a collection of related classes. Throughout the book we will identify library classes and explain how to use them. 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 use this type of interface. Another type of user interface is a Graphical User Interface (GUI), which uses buttons, text fields, and other graphical components for input and output. Java applets use GUIs, as do many Java applications. Because we want to be able to write programs that generate output, 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 statements 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 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 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.14. Note that in this case the PrintStream class has no attributes, just operations or methods. Figure 1.14. 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.15, Java's java.lang.System class contains three predefined streams, including two PrintStream objects. This class has public (+) attributes. None of its public methods are shown here. Figure 1.15. The System class.Both the System.out and System.err objects can be used to write output to the console. As its name suggests, the err stream is used primarily for error messages, whereas the 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 System.out.print("hello"); would produce the following output: hellohello again 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 Java application program is shown in Figure 1.16. Figure 1.16. The OldMacDonald.java class.
This example illustrates the importance of using the Java class library. If there's a 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 it. If so, we can use methods provided by the expert to perform the task in question. Effective Design: Using the Java Library
Self-Study Exercises
|
Tuesday, December 15, 2009
Section 1.6. From the Java Library: System and PrintStream
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment