JPasswordField

Hi,I am just curious as to what is the advantage of returning a char[] vs. a String as with the JPasswordField.getPassword() method? This seems to cause me, as a developer, more codes that I need to do.Thanks....
[233 byte] By [cleya] at [2007-10-2 10:08:43]
# 1
String is immutable and char array is not.Read the API documentation.
hiwaa at 2007-7-13 1:27:11 > top of Java-index,Desktop,Core GUI APIs...
# 2

I am still curious, how often will you "change" the password text? Isn't it more likely that you will do it with "regular" JTextField's input? Why is the regular JTextField.getText() method returns a String then?

Yes, a String is immutable, but realistic speaking, you can still make String changes, it is just that it is less efficient because it really means that the program will allocate a new string whenever you make a change. So, I am still wondering why the Swing team decide to use char[] for the return value instead of String?

cleya at 2007-7-13 1:27:11 > top of Java-index,Desktop,Core GUI APIs...
# 3
> you can still make String changesNo. You can't, never. You can safely, reliably and explicitly delete, or erase, a char[].
hiwaa at 2007-7-13 1:27:11 > top of Java-index,Desktop,Core GUI APIs...
# 4
> I am still wondering why the Swing team decide to use char[] for the return value instead of String?<guess>perhaps it was considered more efficient when creating a valid passwordeg must have at least 5 alpha and 3 numeric
Michael_Dunna at 2007-7-13 1:27:11 > top of Java-index,Desktop,Core GUI APIs...
# 5

Most security system accept passwords as an array of chars. Strings are objects and setting an instance variable to null, which before referenced a passowrd String, would leave the password hanging around in memory until garbage collection.

Using an array of char allows you to properly clean up in iterating through the array and assigning an empty value for each char. This way no trace is left of the password that could otherwise show in a dump.

Frank

fnimphiua at 2007-7-13 1:27:11 > top of Java-index,Desktop,Core GUI APIs...
# 6
Thnaks everyone...I think Frank's answer makes a lot of sense.
cleya at 2007-7-13 1:27:11 > top of Java-index,Desktop,Core GUI APIs...