![]() ![]() ![]() ![]() |
Using System Resources |
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
System.in
- 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. The example program in The Nuts and Bolts of the Java Language uses the standard input stream to count the number of characters typed in by the user. Visit The Standard Input Streamin that lesson for a discussion about the standard input stream.
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.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()
, andwrite()
.The
print()
andprintln()
methods are essentially the same; they both write their String argument to the stream. The one difference between the two methods is thatprintln()
appends a newline character to the end of its output whileprint()
does not. In other words, thisis equivalent to thisSystem.out.print("Duke is not a penguin!\n");Notice the extraSystem.out.println("Duke is not a penguin!");\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 theprint()
methods, and is used to write bytes to the stream. Usewrite()
to write non-ASCII data.Arguments to print() and println()
Theprint()
andprintln()
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 ofprintln()
which takes no arguments and just prints a newline to the stream.Printing Objects of Different Data Types
The following program usesprintln()
to output data of various types to the standard output stream.The program listed above produces this output: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); } }Notice that you can print an object--the firstobject = Thread[Thread-4,5,main] string = Java Mania character array = abc integer = 4 long = -9223372036854775808 float = 3.40282e+38 double = 3.14159 boolean = trueprintln()
method call prints a Thread object and the second prints a String object. When you useprint()
orprintln()
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 formatThreadClass[name,priority,group]See Also
java.io.PrintStream
Input and Output Streams
![]()
Using System Resources