stdin, stdout, and stderr, respectively, in UNIX, being bound to the standard input, standard output and standard error streams. Typically for interactive processes, these streams correspond to the keyboard and screen. (While standard output and standard error are bound to the same device, they are logically different and very useful with stream redirection.)
The System class belongs to the java.lang package, but due to heavy use of this package, it is implicitly imported in every Java program unit. The commands in, out and err are class variables of the System class, and they have been pre- initialized with streams for input and output, respectively. All this may sound ex- ceedingly strange, but it is fruitful to get the concepts clear at this stage as we begin to probe and use the Java API.
The simplest start to input and output are pre-initialized objects System.in, System.out, and System.err. These correspond to the three standard file descriptors
Input and Output Operations 139
Field Summary
static PrintStream err The “standard” error output stream.
static InputStream in The “standard” input stream.
static PrintStream out The “standard” output stream.
If we look up the System class documentation, out is documented as:
public static final PrintStream out;
Looking up the PrintStream class documentation will reveal (not exclusive) the following methods for PrintStream instances:
print(boolean) print(char) print(char[]) print(double) print(float) print(int) print(long) print(Object) print(String)
Thus, the following statements are legitimate:
System.out.print(’g’);
System.out.print(3.142);
System.out.print(45);
System.out.print(”hello there”);
System.out.println(23);
System.out.println(3.32123);
System.out.println(234567654);
System.out.println();
System.out.println(System.out);
In addition, the println() method may also accept similar parameters as print() and with similar behavior, except that a carriage return is also printed after the value.
booleanThis shows that a , char, chararrayPrintStream, double, float instance (e.g., , int, long, System.outObject and ) can print outString values.
140 Object-Oriented Programming and Java
The command println() without any parameters will just print a carriage return.
Note that Object instances are also legitimate parameters to print() and println(). Since all classes are (directly or indirectly) derived from Object, println() will print out any object. In practice, the actual value displayed depends on whether appropriate code is present to provide a suitable textual representation of the object concerned.1
Since System.err is also a static variable initialized as a PrintStream in- stance, it has the same behavior as System.out. It even prints to the same device, that is, the screen, but may be redirected to another via operating system facilities.
The System.in stream is quite different because its use is for input. Again, the documentation for the System class will reveal that it is a static variable that is ini- tialized to an InputStream object.
public static final InputStream in;
Upon further checking of the documentation for the InputStream class, the principal statement of interest is revealed to be reading a byte from the stream.
System.in.read()
As this method returns the internal representation of the character itself, we typically typecast it to a char type by using the ( ) typecast operator.
char c = (char) System.in.read();
Two significant points are noted from the detailed documentation: first, read() potentially throws an IOException object to indicate an error in the input operation.
As such, it is expected that clients using this method must catch the exception within a try-block. Next, the method returns an integer value of –1 after it encounters the last character to be read. Clients must also anticipate against reading past this point.
Putting it all together, we can write a program that reads its input and copies the contents to the output. We put all this code into the static void main() function.
1 The print() method relies on the toString() method to provide a textual representation of an object. Since toString() is defined in the Object class and cannot anticipate properties of future class definitions, it only performs generic text conversion. Of course subclasses are free to override toString() with a more appropriate definition to provide more com- prehensive details of the object.
Input and Output Operations 141
The code shows the following points:
• A try-block anticipates the IOException object from read(), as fore- warned in the signature for read() or its equivalent API documentation.
• A while-statement is used to iteratively read all characters until the end- of-stream as indicated by the –1 sentinel.
• The local variable x is initialized to 0, and is incremented each time in the loop to count the number of bytes read (and written).
• Output is written into two logical streams. The output stream contains that which was read from the input stream, while the error stream is for diagnostic messages of byte count, or errors. (If any stream is redirected, the other proceeds with the original device binding.)