What is System.in and System.out?

What is System.in and System.out?

Every language supports both input (like scanf()) and output (like printf()). Java supports two types of input (from command-prompt and from GUI) and two types of output (at command prompt and at GUI). The statements System.in System.out are of Java for command-prompt.

System.in is used to take input from command prompt and System.out is used for output at command prompt. Every Operating System comes with three types builtin streams used by OS. A stream is nothing but a carrier of data from one place to another. The streams are:

1. in: in stream carries data from Keyboard to CPU. Any data you type at Keyboard like notepad is carried to CPU by this stream. Any language would like to read from Keyboard should connect to in stream. Java connects through System class as System.in. That is System.in is implicitly connected to input mechanism of underlying OS.
2. out: out stream of OS carries data from CPU to command prompt. Any language for output should connect to this stream like Java connects as System.out.
3. err: err stream is also same purpose of carrying data from CPU to Monitor but used by OS for displaying error messages while Operating System is being booted. Of course, Java Programmer can also use this like out stream like System.err.

The following Example uses all these streams as System.in System.out and System.err.

System.in System.outOutput Screenshot on System.in System.out

Scanner scan1 = new Scanner(System.in);

Observe, System.in is connected to Scanner to read input from Keyboard. Now scan1 is ready to take Keyboard input. To facilitate, Scanner class comes with many builtin methods like nextLine(), nextInt() and nextDouble() etc returning a string, int and double values.

System.out.println(“Circle Perimetetr: ” + 2*Math.PI*radius);
System.err.println(“Rectangle Area: ” + length*height)

Observe, System.out and System.err. Both print values at command prompt.

More is discussed in the following links.

1. Do you know what is System.in?
2. Do you know what is System.out?

Leave a Reply

Your email address will not be published. Required fields are marked *