In Java, the standard input/output (I/O) streams are used to read and write data to and from the console or command line. The standard input stream is referred to as System.in and the standard output and error streams are referred to as System.out and System.err respectively.
Here’s a more detailed explanation of each standard stream:
System.in
The standard input stream is an input stream that is connected to the console or command line. It allows you to read input from the user. Here’s an example:
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your name:");
String name = scanner.nextLine();
System.out.println("Hello, " + name + "!");
In this example, we are using the Scanner class to read input from the user via the standard input stream (System.in). We prompt the user to enter their name, and then read the input using the nextLine() method. Finally, we print a greeting message to the console using the standard output stream (System.out).
System.out
The standard output stream is an output stream that is connected to the console or command line. It allows you to write output to the user. Here’s an example:
System.out.println("Hello, World!");
In this example, we are using the println() method to write the string “Hello, World!” to the console via the standard output stream (System.out).
System.err
The standard error stream is an output stream that is connected to the console or command line. It allows you to write error messages to the user. Here’s an example:
System.err.println("Error: Something went wrong!");
In this example, we are using the println() method to write the error message “Error: Something went wrong!” to the console via the standard error stream (System.err).
There are many more classes and methods available for reading and writing data in Java, so be sure to check out the Java documentation for more information.