The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: Accessing System Resources

The Standard I/O Streams

The concept of standard input and output streams is a C library concept that has been assimilated into the Java environment. There are three standard streams, all of which are managed by the java.lang.System class:
Standard input--referenced by
Used for program input, typically reads input entered by the user.
Standard output--referenced by System.out
Used for program output, typically displays information to the user.
Standard error--referenced by System.err
Used to display error messages to the user.

Standard Input Stream

The System class provides a stream for reading text--the standard input stream.

[PENDING: put in an example and more description or a pointer or something]

Standard Output and Error Streams

Probably the most often used items from the System class are the the standard output and standard error streams, which you use to display text to the user. The standard output stream is typically used for command output, to display the results of a command to the user. The standard error stream is typically used to display any errors that occur when a program is running.

The print, println, and write Methods

Both standard output and standard error derive from the PrintStream class. Thus, you use one of PrintStream's three methods to print text to the stream: print, println, and write.

The print and println methods are essentially the same; they both write their String argument to the stream. The one difference between the two methods is that println appends a newline character to the end of its output while print does not. In other words, this

System.out.print("Duke is not a penguin!\n");
is equivalent to this
System.out.println("Duke is not a penguin!");
Notice the extra \n in the first method call; it's the two-character code for a newline character. println automatically appends a newline character to its output.

The write method is less frequently used than either of the print methods, and is used to write bytes to the stream. Use write to write non-ASCII data.

Arguments to print and println

The print and println methods both take a single argument. The argument may be one of any of the following data types: Object, String, char[], int, long, float, double, and boolean. In addition, there's an extra version of println that takes no arguments and just prints a newline to the stream.

Printing Objects of Different Data Types

The following program uses println to output data of various types to the standard output stream.
public class DataTypePrintTest {
    public static void main(String[] args) {

        Thread objectData = new Thread();
        String stringData = "Java Mania";
        char[] charArrayData = { 'a', 'b', 'c' };
        int integerData = 4;
        long longData = Long.MIN_VALUE;
        float floatData = Float.MAX_VALUE;
        double doubleData = Math.PI;
        boolean booleanData = true;

The program listed above produces this output:
Java Mania
Notice that you can print an object--the first println method call prints a Thread object and the second prints a String object. When you use print or println to print an object, the data printed depends on the type of the object. In the example, printing a String object yields the contents of the String. However, printing a Thread yields a string of this format:

For a thorough discussion of I/O streams in Java, refer to I/O: Reading and Writing (but no 'rithmetic) (in the Essential Java Classes trail).

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.