Why is char[] preferred over String for passwords?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Java developers are often told to store passwords in char[] instead of String. The advice is not about syntax style. It is about reducing how long sensitive data stays readable in memory and how easily that data can be copied or exposed.
Why String Is a Poor Container for Secrets
A String is immutable. Once created, its characters cannot be changed. That makes strings convenient for general programming, but it is a bad property for passwords because you cannot actively erase the contents after authentication finishes.
If a password is stored in a String, the text stays in memory until the garbage collector eventually reclaims that object. You do not control when that happens. During that time, the value can appear in heap dumps, debugger inspections, crash reports, or accidental logging.
A char[] is mutable, so code can overwrite the characters immediately after use. That does not make the password magically safe, but it shortens the exposure window.
What char[] Lets You Do
The main advantage is explicit cleanup. After validating the password, you can replace each character with a neutral value.
The important line is Arrays.fill(password, '\0'). With a String, there is no equivalent operation.
A Common Real-World Pattern
Swing uses this idea directly. JPasswordField returns char[] from getPassword() instead of returning a String.
That API design exists for a reason: UI frameworks do not want to force secret input into an immutable string.
Security Benefit and Its Limits
Using char[] is a defensive improvement, not a full security system. The password may still be copied internally by libraries, converted into bytes for hashing, or captured elsewhere in the program. If you later do new String(password), you lose the benefit because you create another immutable copy.
It is also worth being precise about the risk. The problem is not that every String goes into the string pool. Passwords read from input are usually ordinary heap objects, not interned literals. The real issue is immutability and uncontrolled lifetime, not automatic interning.
Better Workflow for Password Handling
A reasonable flow in Java looks like this:
- Read the password into
char[]. - Hash or verify it immediately.
- Clear the array in a
finallyblock. - Avoid converting it to
Stringunless an API leaves no alternative.
Here is a simple example using MessageDigest after converting to bytes as late as possible:
This example still creates a temporary String, so it is not ideal. It shows why secret-handling APIs often accept char[] directly when possible.
Common Pitfalls
Developers often switch to char[] but then immediately print it, log it, or convert it into String for convenience. That defeats the purpose.
Another common mistake is forgetting cleanup when exceptions occur. If you only clear the array on the success path, the password remains in memory during the error path, which is exactly where debugging tools and dumps are most likely.
Finally, do not oversell the rule. char[] reduces exposure, but secure password handling still depends on hashing, transport security, careful logging, and avoiding unnecessary copies.
Summary
- '
Stringis immutable, so secret text cannot be erased after use.' - '
char[]can be overwritten, which shortens how long a password remains readable in memory.' - The main benefit is lifecycle control, not performance.
- Converting a password back to
Stringremoves most of the advantage. - Clear password arrays in a
finallyblock so cleanup also happens during failures.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.