System.in System.out System.err

System.in System.out System.err

Every OS comes with three built in streams – in, out and err entirely managed by OS. We know from IO Streams that a stream is a flow of data between two terminals (may be two files or two sockets etc.). in stream carries (flows) data from Keyboard to CPU. out stream carries data from CPU to console (command prompt). err stream is used by OS to display error messages while the OS is getting booted. Any language provides handles to link to these streams. For example, C-lang links through stdin, stdout and stderr. Similarly, Java connects to these streams through System class as System.in, System.out and System.err.

1. About System.in

Now to put it other way, System.in is nothing but an in stream of OS linked to System class. Using System class, we can divert the in stream going from Keyboard to CPU into our program. This is how keyboard reading is achieved in Java.

See the following snippet of code.

  DataInputStream dis = new DataInputStream(System.in);
  System.out.println("Enter your name: ");
  String str = dis.readLine();
  System.out.println("I know your name is " +  str);

System.in is passed as parameter to DataInputStream constructor and thus dis is linked to System.in. readLine() method reads from in stream going from Keyboard to CPU. Other way, the Keyboard stream is diverted into our program. The string str is what you entered your name from keyboard. This is how Java scanf works.

Let us see what Java API says about System.in

  • public static final InputStream in: The "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.

Coming to API, in is an InputStream object defined in System class as static and final object. The in object internally connected to the input mechanism of OS.

For full blown program see Keyboard Input – DataInputStream.

2. About System.out

What API says:

  • public static final PrintStream out: The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.

In API, out is declared as a PrintStream object as static and final. The out object is internally connected to the output mechanism of OS. Any data given to System.out, writes to console (DOS prompt).

  PrintStream ps = new PrintStream(System.out);
  ps.println("Hello");

To pass data to System.out, PrintStream class is used. println() is a method of PrintStream class. Consequently, the string Hello passed to println() method goes to System.out that carries data to DOS prompt. All this is done by JVM automatically.

For full blown program see PrintStream.

3. About System.err

What Java API says about out object, the same is said with err object also.

  • public static final PrintStream err: The "standard" error output stream. This stream is already open and ready to accept output data.

err is also an object of PrintStream connected implicitly to output mechanism of OS. As both out and err has the same status, following both statements do the same job.

  System.out.println("Hello");
  System.err.println("Hello");

To carry good meaning, better use err object with catch block to display an error message.

  try
  {
    System.out.println("Hello 1");
  }
  catch(Exception e)
  {
    System.out.println("Error occurred for the problem of " + e);
  }  

View All for Java Differences on 90 Topics

1 thought on “System.in System.out System.err”

Leave a Comment

Your email address will not be published.