1

how come System.out.println() method prints character on the screen when out is of type print stream which is used to display bytes

leppie
  • 115,091
  • 17
  • 196
  • 297
rishabh
  • 11
  • 2
  • 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 Answers4

11

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
3

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
0

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.

Marko
  • 20,385
  • 13
  • 48
  • 64
Ritesh Kaushik
  • 715
  • 2
  • 13
  • 24
0

System.out is a special PrintStream, who's output is displayed on the console. Check here for more documentation.

nrobey
  • 705
  • 4
  • 12