Tuesday, December 15, 2009

Section 1.6. From the Java Library: System and PrintStream










[Page 52 (continued)]

1.6. From the Java Library: System and PrintStream


JAVA 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.sun.com/docs



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.



[Page 53]

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");
System.out.println("hello again");
System.out.println("goodbye");


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 Java application program is shown in Figure 1.16.




[Page 54]

Figure 1.16. The OldMacDonald.java class.





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




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

Learning how to use classes and objects from the Java class library is an important part of object-oriented programming in Java.




Self-Study Exercises


Exercise 1.3

One good way to learn how to write programs is to modify existing programs. Modify the OldMacDonald class to "sing" one more verse of the song.

Exercise 1.4

Write a Java class that prints the design shown on the left.


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














No comments: