Possible Duplicate:
Why is char[] preferred over string for passwords?
When I was preparing for OCPJP
I came accross the topic - "Reading User input from console".
There was an example where it read username
in String
reference, whereas password
in a char[]
array, but I couldn't understand why it used char array.. Here is the code : -
Console console = System.console();
String username = console.readLine("User Name? ");
char[] password = console.readPassword("Password? ");
This raised a doubt in my mind.. Why didn't we used String reference to store password. Since Strings
are immutable, so it must be more secure to read password in a String, as its content could not be changed for that matter.
So, what's the whole point in reading password
in char[]
array..
Can anyone shed some light in this matter?