SOLVED: Mask password with asterisk from console in Java

Azka Iftikhar



In this article, we will study how to mask password with an asterisk Java console.  masking is an important technique that is used vastly in programming for security purposes like logging in and signing up for entering any pin information.

To mask password with asterisk java console, the console library provides a method that is readPassword method, it reads the password and hides it from the user, the password is stored in a character array which is returned by the method.


Method-1: Use System Console Read Password Method

The first method to mask password with asterisk java console is the console read password function, which is a java built in function used to mask the password, this can further be masked by adding a asterisk string in the code print the asterisk string  the code is  is written below :


// mask password with asterisk java console
class Main {

    public void consoleFunc() {
        Console console = System.console();
        if (console == null) {
            System.out.println("Couldn't get Console instance");

        char[] passwordArray = console.readPassword("Enter your  password: ");
        for (int i = 0; i < passwordArray.length; i++) {
        console.printf("Password entered was: \n", new String(passwordArray));


    public static void main(String[] args) {
        new Main().consoleFunc();

The output is :

Testing password
Enter your secret password: 
Password entered was: azka


Method-2: Use threads to erase characters from password

We can use a thread that can erase the echoed characters, the ones that are used

Here we are using the threading technique to mask password with asterisk java console by hiding the character while typing, the code is


// mask password with asterisk java console
class ThreadDisappear implements Runnable {
    private boolean end;
    public ThreadDisappear(String prompt) {
    public void run() {
        end = true;
        while (end) {
            try {
            } catch (InterruptedException ie) {

    public void maskEnd() {
        this.end = false;



class maskwithsterik {
    public static void main(String[] args) {
        ThreadDisappear td = new ThreadDisappear("Enter your password: ");
        Thread t = new Thread(td);
        BufferedReader br = new BufferedReader(new InputStreamReader(;
        try {
            String password = br.readLine();
            System.out.println("\nYour password is: " + password);
        } catch (IOException ioe) {


The output of this code is :

Enter your password:***************
Your password is: thisispassword


Compare the Above Two Methods

Since Java has no specific API or library to mask password with asterisk Java console so we use different logics to do so we have seen 2 different methods above and the most efficient and less time-consuming is the first one since we are just hiding the password on console, we can also get it whenever we want, but it is a less secure method. Threading may take up more processing time and thus be less efficient.

Although the graphical user interfaces like Java swing and Java effects provide API to hide password fields.  but none of them are applicable to the Java console so we must stick with the traditional logic building here.



We have seen three Different techniques to mask password with asterisk Java console.  the first way is to use the console based masking by using readPassword function and the other is through thread, the thread reads and stops printing asterisks when input thread stops.


Further Reading

Masking Format
Data Masking and Security


Views: 88

Azka Iftikhar

She is proficient in multiple programming languages, including C++, Golang, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can read more on her GitHub page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to

Thank You for your support!!

Leave a Comment

GoLinuxCloud Logo

We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages