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.
import java.util.Scanner;                        // importing only one class
public class InOutErr
{
 public static void main(String args[])
 {
   Scanner scan1 = new Scanner(System.in);       // create Scanner object. Observe, System.in

   System.out.println("Enter Student Name:");    // start taking required inputs
   String stdName = scan1.nextLine();

   System.out.println("Enter Circle Radius as a whole number:");
   int radius = scan1.nextInt();

   System.out.println("Enter Recgtangle length, you can enter floating-point value:");
   double length = scan1.nextDouble();

   System.out.println("Enter Recgtangle height, you can enter floating-point value:");
   double height = scan1.nextDouble();
                                                 // display the results of computation
   System.out.println("\nStudent Name:" + stdName);
   System.out.println("Circle Perimetetr: " + 2*Math.PI*radius);        // observe, System.out
   System.err.println("Rectangle Area: " + length*height);              // observe, 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 Comment

Your email address will not be published.