System.out.println() / System.err.println() / System.in
A class that provides access to standard output, standard error output, and standard input. System.out.println() is the most commonly used method for debugging and logging output.
Syntax
// Prints to standard output with a newline. System.out.println(value); // Prints to standard output without a newline. System.out.print(value); // Prints with format specifiers (similar to printf). System.out.printf(String format, Object... args); // Prints to standard error output (displayed in red). System.err.println(value); System.err.print(value); // Reads keyboard input from standard input. Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); // Reads one line. int num = scanner.nextInt(); // Reads an integer. double d = scanner.nextDouble();// Reads a decimal number. scanner.close(); // Close the scanner when done.
Main Methods
| Method | Description |
|---|---|
| System.out.println(x) | Prints the string representation of x to standard output followed by a newline. Passing null prints "null". |
| System.out.print(x) | Prints without a trailing newline. |
| System.out.printf(format, args) | Formats the output using a format string. No automatic newline is appended. |
| System.err.println(x) | Prints to standard error output. Used for logging error messages. |
| scanner.nextLine() | Reads one line from standard input and returns it as a string. |
| scanner.nextInt() | Reads a space- or newline-delimited integer from standard input. |
Sample Code
// Print values of various types using println().
System.out.println("Hello"); // Prints: Hello
System.out.println(42); // Prints: 42
System.out.println(3.14); // Prints: 3.14
System.out.println(true); // Prints: true
System.out.println(null); // Prints: null
// Print without newlines using print().
System.out.print("A");
System.out.print("B");
System.out.println("C"); // Prints: ABC on a single line
// Print with formatting using printf().
String name = "Alice";
int age = 30;
System.out.printf("Name: %s, Age: %d%n", name, age); // Prints: Name: Alice, Age: 30
System.out.printf("Pi: %.4f%n", Math.PI); // Prints: Pi: 3.1416
// Print an error message using err.println().
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.err.println("An error occurred: " + e.getMessage());
}
// Read input from standard input using Scanner.
import java.util.Scanner;
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String input = sc.nextLine();
System.out.println("Hello, " + input + "!");
sc.close();
Notes
System.out is a static field of type PrintStream connected to standard output. Similarly, System.err is connected to standard error output, so error messages flow through a separate stream from System.out.
Common format specifiers are %s (string), %d (integer), %f (floating-point), %.2f (two decimal places), and %n (newline). In production applications, it is recommended to use a logging library such as java.util.logging or SLF4J instead of System.out.println().
For timing measurements, see System.currentTimeMillis() / System.nanoTime().
If you find any errors or copyright issues, please contact us.