Writing
Console Output
Console output is most easily
accomplished with print( ) and println( ), described earlier, which
are used in most of the examples in this book. These methods are defined by the
class PrintStream (which is the type
of object referenced by System.out).
Even though System.out is a byte
stream, using it for simple program output is still acceptable. However, a character-based alternative is
described in the next section.
Because PrintStream is an output stream derived from OutputStream, it also implements the low-level method write( ). Thus, write( ) can be used to write to the console. The simplest form of write( ) defined by PrintStream is shown here:
void write(int byteval)
This method writes the byte
specified by byteval. Although byteval is declared as an integer, only
the low-order eight bits are written. Here is a short example that uses write( ) to output the character
"A" followed by a newline to the screen:
// Demonstrate System.out.write().
class WriteDemo {
public static void main(String args[]) { int b;
b = 'A'; System.out.write(b); System.out.write('\n');
}
}
You will not often use write( ) to perform console output
(although doing so might be useful in some situations) because print( ) and println( ) are substantially easier to use.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.