- Strings are immutable: Strings are immutable in Java and therefore if a password is stored as plain text it will be available in memory until Garbage collector clears it and as Strings are used in String pool for re-usability there are high chances that it will remain in memory for long duration, which is a security threat. Strings are immutable and there is no way that the content of Strings can be changed because any change will produce new String.
With an array, the data can be wiped explicitly data after its work is complete. The array can be overwritten and and the password won’t be present anywhere in the system, even before garbage collection. - Security: Any one who has access to memory dump can find the password in clear text and that’s another reason to use encrypted password than plain text. So Storing password in character array clearly mitigates security risk of stealing password.
- Log file safety: With an array, one can explicitly wipe the data , overwrite the array and the password won’t be present anywhere in the system.
With plain String, there are much higher chances of accidentally printing the password to logs, monitors or some other insecure place. char[] is less vulnerable.//Java program to illustate prefering char[] arrays//over strings for passwords in JavapublicclassPasswordPreference{publicstaticvoidmain(String[] args){String strPwd ="password";char[] charPwd =newchar[] {'p','a','s','s','w','o','r','d'};System.out.println("String password: "+ strPwd );System.out.println("Character password: "+ charPwd );}}chevron_rightfilter_noneOutput:
String password: password Character password: [C@15db9742
- Java Recommendation: Java has methods like JPasswordField of javax.swing as the method public String getText() which returns String is Deprecated from Java 2 and is replaced by public char[] getPassword() which returns Char Array.
This article is contributed by Kanika Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java Guava | Chars.indexOf(char[] array, char[] target) method with Examples
- Java Guava | Chars.indexOf(char[] array, char target) method with Examples
- Java Program to Generate N Number of Passwords of Length M Each
- How to encrypt passwords in a Spring Boot project using Jasypt
- When to use StringJoiner over StringBuilder?
- Why is Scanner skipping nextLine() after use of other next functions?
- Move all Uppercase char to the end of string
- Iterating over Arrays in Java
- Iterating over ArrayLists in Java
- Iterating over Enum Values in Java
- Program to Iterate over a Stream with Indices in Java 8
- How to loop over TreeSet in Java
- How to iterate over a TreeMap in Java?
- How to iterate over a 2D list (list of lists) in Java
- Java Program to Iterate Over Arrays Using for and foreach Loop
- PrintWriter print(char[]) method in Java with Examples
- PrintWriter print(char) method in Java with Examples
- PrintWriter write(char[]) method in Java with Examples
- PrintWriter write(char[], int, int) method in Java with Examples
- PrintWriter println(char) method in Java with Examples

