Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
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 thejava.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.
TheSystem
class provides a stream for reading text--the standard input stream.[PENDING: put in an example and more description or a pointer or something]
Probably the most often used items from theSystem
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 thePrintStream
class. Thus, you use one ofPrintStream
's three methods to print text to the stream:println
, andwrite
.The
println
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 whileis 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 thewrite
to write non-ASCII data.Arguments to print and println
Theprintln
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: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; 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 useprintln
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]For a thorough discussion of I/O streams in Java, refer to I/O: Reading and Writing (but no 'rithmetic).
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.