Previous | Next | Trail Map | The Java Development Environment | Using System Resources


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 can use write text to the user's terminal window. Both the standard output and standard error streams are System class variables. You can address them with System.out and System.err, respectively.

The standard output stream is typically used for command output, that is, to print the results of a command to the user. The standard error stream is typically used to print any errors that occur when a program is running. Printing program output and errors to different streams allows the user to pipe them to different locations thereby separating them.

The print(), println() and write() Methods

Both standard output and standard error derive from the PrintStream class. As such, 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, and because the Java language supports method overloading, 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() which 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.
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;

	System.out.println("object = " + ObjectData);
	System.out.println("string = " + StringData);
	System.out.println("character array = " + CharArrayData);
	System.out.println("integer = " + IntegerData);
	System.out.println("long = " + LongData);
	System.out.println("float = " + FloatData);
	System.out.println("double = " + DoubleData);
	System.out.println("boolean = " + BooleanData);
    }
}
The program listed above produces this output:
object = Thread[Thread-4,5,main]
string = Java Mania
character array = abc
integer = 4
long = -9223372036854775808
float = 3.40282e+38
double = 3.14159
boolean = true
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
ThreadClass[name,priority,group]

See Also

java.io.PrintStream


Previous | Next | Trail Map | The Java Development Environment | Using System Resources