-1

I have two questions related to the System class of java.

First, where are in (input) and out (output) objects initialized? As these objects are declared as static and not initialized in the System class, and System class does not have a constructor.

The second question is, in (input) object is declared as input stream that is why it reads byte data and the out object is also outputStream then why it is able to write all type of primitive data. Stream is used to read/write bytes data. Do not say it calls the toString method.

erickson
  • 265,237
  • 58
  • 395
  • 493

1 Answers1

3

The System class members in, out, and err are initialized in the private function initializeSystemClass(). This function is invoked by the JVM initialization procedures.

The member out is not only an OutputStream. It is a PrintStream, and has an extended API. All of the print() methods that accept arguments of primitive type use the corresponding String.valueOf() methods to create a String object representing the value. These strings are then funneled through an OutputStreamWriter instance that encodes text into bytes using a specific character encoding. The resulting bytes are written to the appropriate file descriptor.

erickson
  • 265,237
  • 58
  • 395
  • 493