+ 1
Why char array is preferred to store password than String in Java?
That's what I wanna know why can't store in an array
1 Answer
+ 5
Strings are immutable . That means once you've created the
String, if another process can dump memory, there's no way (aside from reflection ) you can get rid of the data before garbage collection kicks in.
With an array, you can explicitly wipe the data after you're done with it. You can overwrite the array with anything you like, and the password won't be present anywhere in the system, even before garbage collection.
So yes, this is a security concern - but even using char[] only reduces the window of opportunity for an attacker, and it's only for this specific type of attack.