In Java, Standard Input and Standard Output are the primary ways to read data from the user and display information to the console. These are managed through the java.lang
package using the following classes and streams:
- Standard Output: Uses
System.out
for displaying data. - Standard Input: Uses
System.in
for taking input.
1. Standard Output (System.out)
The System.out
object is a PrintStream connected to the console. It provides several methods to print data, including:
print()
: Prints data without a newline.println()
: Prints data followed by a newline.printf()
: Prints formatted strings.
Syntax:
System.out.println("Hello, World!");
System.out.print("Java Programming");
System.out.printf("Name: %s, Age: %d", "Mahesh", 25);
Code language: CSS (css)
Example: Printing Messages
public class StandardOutputExample { public static void main(String[] args) { System.out.println("Welcome to Java!"); System.out.print("Learning "); System.out.print("Standard Output."); System.out.printf("\nYour age is %d years.", 9); } } /* Welcome to Java! Learning Standard Output. Your age is 9 years. */
2. Standard Input (System.in)
The System.in
object is an InputStream that reads bytes from the keyboard. To convert bytes into characters, we often use the Scanner
class.
Using Scanner for Input
To read different types of data:
nextInt()
: Reads an integer.nextDouble()
: Reads a double.nextLine()
: Reads a line of text.next()
: Reads a single word.
Syntax:
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
String name = sc.nextLine();
Code language: JavaScript (javascript)
Example: Reading User Input
import java.util.Scanner; public class StandardInputExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your name:"); String name = sc.nextLine(); System.out.println("Enter your age:"); int age = sc.nextInt(); System.out.printf("Name: %s, Age: %d%n", name, age); sc.close(); } } /* Enter your name: Mahesh Enter your age: 25 Name: Mahesh, Age: 25 */
3. Handling Different Data Types
The Scanner
class also supports reading various primitive types:
nextByte()
nextShort()
nextLong()
nextFloat()
nextDouble()
nextBoolean()
Example: Different Data Types
import java.util.Scanner; public class MultiInputExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter an integer:"); int num = sc.nextInt(); System.out.println("Enter a floating-point number:"); float fnum = sc.nextFloat(); System.out.println("Enter a word:"); String word = sc.next(); System.out.printf("Integer: %d, Float: %.2f, Word: %s%n", num, fnum, word); sc.close(); } }
4. Reading Multiple Words
Sometimes, we need to read a complete line rather than a single word. In such cases, use nextLine()
.
Example:
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
String sentence = sc.nextLine();
System.out.println("You entered: " + sentence);
Code language: JavaScript (javascript)
5. Handling Input Errors
When users enter the wrong data type, a java.util.InputMismatchException
may occur.
Example: Handling Errors
try {
System.out.println("Enter an integer:");
int number = sc.nextInt();
} catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter an integer.");
}
Code language: JavaScript (javascript)
6. Using BufferedReader for Efficient Input
The BufferedReader
class can also be used for input, especially when fast reading is needed.
Example: Using BufferedReader
import java.io.*; public class BufferedReaderExample { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a line:"); String line = br.readLine(); System.out.println("You entered: " + line); } } /* Enter a line: Hello, Java! You entered: Hello, Java! */
7. Printing Errors (System.err)
The System.err
stream is used to print error messages. It works similarly to System.out
but is intended for error output.
Example: Printing Errors
public class ErrorPrintingExample { public static void main(String[] args) { System.err.println("Error: Invalid operation."); } } /* Error: Invalid operation. */
8. Difference between System.out, System.err, and System.in

9. Closing Resources
It is essential to close resources like Scanner
and BufferedReader
to avoid memory leaks.
Example:
Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter your age:");
int age = sc.nextInt();
System.out.println("Your age is: " + age);
} finally {
sc.close();
}
Code language: JavaScript (javascript)
Java’s System.out
, System.err
, and System.in
are fundamental for standard input and output operations. While System.out
and System.err
handle output (normal and error, respectively), System.in
is used for input. The Scanner
class is typically used to read input efficiently, while BufferedReader
is preferable when performance is crucial.