I never thought before, only I used the method getPassword
that returning an array of characters and I had seen the getText
method was deprecated. But now that I think, why this method was deprecated?.
The Java documentation explains:
Deprecated. As of Java 2 platform v1.2, replaced by
getPassword
.Fetches a portion of the text represented by the component. Returns an empty string if length is 0.
For security reasons, this method is deprecated. Use the
getPassword
method instead.
But what are those security reasons? Any ideas about this?
Thank you in advance.
Fetches a portion of the text represented by the component. Returns an empty string if length is 0. For security reasons, this method is deprecated.
it actually returns Editable and not CharSequence but you can store it in a String variable by calling toString() on it.
When calling getText
you get a String (immutable object) that may not be changed (except reflection) and so the password stays in the memory until garbage collected.
When calling getPassword
you get a char array that may be modified, so the password will really not stay in memory.
Try this :
String myPass=String.valueOf(passwordField.getPassword());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With