The Console class in Java (available in the java.io package) provides methods to access the character-based console device — typically, the text terminal from which the user runs the program. It’s useful for reading input and writing output in a secure and interactive way, especially for password input where echoing is disabled.
The Console class cannot be instantiated manually.You retrieve a Console object using:
Console console = System.console();Code language: JavaScript (javascript)
Commonly Used Methods

Simple Program: Basic Console Input
Mahesh wants to enter his name and age using the command-line console. LotusJavaPrince needs to securely read it and display it.
import java.io.Console;
import java.util.Arrays;
public class SecureLogin {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run from a terminal.");
return;
}
String username = console.readLine("Enter username: ");
char[] password = console.readPassword("Enter password: ");
// Simulate check (in a real app, don't use plain text!)
if (username.equals("Mahesh") && Arrays.equals(password, "lotus123".toCharArray())) {
console.printf("Login successful!\n");
} else {
console.printf("Invalid credentials.\n");
}
// Always clear password from memory
Arrays.fill(password, ' ');
}
}
Output
Enter username: Mahesh
Enter password:
Login successful!
The Console class is ideal for interactive applications requiring user input via terminal.
- It enhances security by allowing password input with echo suppression.
- It provides formatted input/output using familiar printf-style formatting.
- It’s not usable inside most IDEs; must be run from a real terminal.
