how come System.out.println() method prints character on the screen when out is of type print stream which is used to display bytes
-
It's probably be best if you could elaborate on your question. I could approach answering this a number of ways and I'm not sure which way is appropriate. I also don't want to be accidentally offensive with my answer... – Frank V Jan 23 '11 at 20:02
4 Answers
PrintStream
was introduced in Java 1.0 and used in among others System.out
. Later they realized that it was a major mistake to use platform default encoding to convert bytes to characters, so they introduced PrintWriter
later with Java 1.1 which is able to accept an OutputStreamWriter
wherein you can specify the character encoding. It was however too late then to change System.out
.

- 1,082,665
- 372
- 3,610
- 3,555
I guess this piece of code (from java.lang.System
) explains it:
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
It is creating a FileOutputStream
to the standard out, and then wraps it in a PrintStream
. FileDescriptor.out
is "a handle to the standard output stream".
And it is converting bytes to characters using the platform default encoding.

- 588,226
- 146
- 1,060
- 1,140
PrintStream
is a byte stream and PrintWriter
is a character stream, but at the lowest level everything is byte oriented, I have read somewhere that each PrintStream
incorporates an OutputStreamWriter
, and it passes all characters through this writer to produce bytes for output.

- 20,385
- 13
- 48
- 64

- 715
- 2
- 13
- 24