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 and Output Streamsin 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 to display text to the user.The standard output stream is typically used for command output, that is, 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()
, 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. The argument may be one of any of the following data types: Object, String,char[]
,int
,long
,float
,double
, andboolean
. In addition, there's an extra version ofprintln()
that 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(objectData); System.out.println(stringData); System.out.println(charArrayData); System.out.println(integerData); System.out.println(longData); System.out.println(floatData); System.out.println(doubleData); System.out.println(booleanData); } }Notice that you can print an object--the firstThread[Thread-4,5,main] Java Mania abc 4 -9223372036854775808 3.40282e+38 3.14159 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