Console Class in Java
The Console class in Java, in java.io, gives access to the terminal (command prompt) the program was started from. It is used to read lines typed by the user, and especially to read passwords without showing them on the screen. You get the console with System.console(), which returns null when the program has no terminal, for example when it runs inside an IDE or with its input redirected from a file. In this lesson, you will learn the methods of Console and how to handle a missing console.
Declaration
public final class Console implements FlushableMethods of Console
| Method | Description |
|---|---|
String readLine() | It is used to read one line of text. |
String readLine(String fmt, Object... args) | It shows a formatted prompt, then reads a line. |
char[] readPassword() | It reads a password without echoing the characters. |
char[] readPassword(String fmt, Object... args) | It shows a prompt, then reads a password. |
Console printf(String fmt, Object... args) | It writes formatted text to the terminal. |
PrintWriter writer() / Reader reader() | It returns the console's writer or reader. |
boolean isTerminal() | It returns true if connected to an interactive terminal (Java 22). |
Example: A Login Prompt
Let's see an example that reads a username and a hidden password.
import java.io.Console;
import java.util.Arrays;
public class Login {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console: run this program from a terminal.");
return;
}
String user = console.readLine("Username: ");
char[] password = console.readPassword("Password for %s: ", user);
console.printf("Welcome, %s! Your password has %d characters.%n",
user, password.length);
Arrays.fill(password, ' ');
}
}No console: run this program from a terminal.When the example runner executes it, there's no terminal, so System.console() returns null. Run from a real terminal, typing Navika and a 9-character password, the session looks like this (the password isn't shown as it's typed):
Username: Navika
Password for Navika:
Welcome, Navika! Your password has 9 characters.Explanation: The program first checks for null, the safe habit. readLine shows a prompt and reads a line. readPassword turns off echoing, so nothing appears while the password is typed. It returns a char[] instead of a String, so the program can wipe the password from memory with Arrays.fill as soon as it's no longer needed.
Why char[] for Passwords?
A String is immutable and may stay in memory until the garbage collector removes it, possibly visible in a memory dump. A char[] can be overwritten immediately with Arrays.fill, so the password lives in memory for as short a time as possible.
Common Mistakes
Calling System.console().readLine() without a null check. It throws NullPointerException inside IDEs.
Converting the password to a String. That defeats the purpose of char[].
Expecting Console to work when input is piped from a file. Use System.in for that.
Practice
- Easy: Write a program that asks for a name with
Console.readLineand greets the user withprintf, falling back toScannerwhen the console isnull. - Harder: Ask for a new password twice with
readPassword, compare the twochar[]arrays withArrays.equals, and clear both arrays afterwards.
Key Takeaways
Was this tutorial helpful?