Lost your password? Please enter your email address. You will receive a link and will create a new password via email.


You must login to ask a question.

You must login to add post.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

RTSALL Latest Articles

Why is char[] preferred over String for passwords?

Why is char[] preferred over String for passwords?

It is generally recommended to use a character array (char[]) instead of a string (String) to store passwords in Java, due to the way that strings are handled in memory.

Strings in Java are immutable, meaning that once a string is created, it cannot be changed. When a password is stored as a string, the contents of the password are stored in memory as a series of characters. The memory location where the string is stored can be accessed by other parts of the application, and the contents of the password string can be seen by unauthorized parties.

In contrast, when a password is stored as a character array, the password is stored in a mutable form, and the contents can be overwritten or erased when they are no longer needed. Additionally, the memory where the password is stored can be explicitly cleared after use, reducing the risk that the password contents will be visible to unauthorized parties.

In summary, using a character array to store passwords provides a more secure method of storing password data, as it reduces the risk of the password contents being accessed by unauthorized parties, and enables the password to be erased from memory after use.

Here’s an example of how you might store a password using a character array in Java:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter password: ");
        char[] password = scanner.nextLine().toCharArray();
        // Do something with the password, such as authenticating a user
        // ...
        // Clear the password from memory
        for (int i = 0; i < password.length; i++) {
            password[i] = '\0';
        }
    }
}

In this example, the password is entered by the user, and stored in a character array. After the password is used, the contents of the array are overwritten with the null character ('\0') to clear the password from memory.

Here's an example of how you might store a password using a string:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter password: ");
        String password = scanner.nextLine();
        // Do something with the password, such as authenticating a user
        // ...
        // The password remains in memory, and cannot be explicitly erased
    }
}

In this example, the password is stored in a string and remains in memory after the password has been used. While the password can be erased by the garbage collector at some point in the future, there is no way to explicitly erase the password from memory.

It’s important to note that even when using a character array, it’s still necessary to take other security measures to protect password data, such as using secure encryption algorithms and avoiding hardcoding passwords in code.

Related Posts

Leave a comment

You must login to add a new comment.