Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why getText() in JPasswordField was deprecated?

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.

like image 614
Paul Vargas Avatar asked May 04 '12 05:05

Paul Vargas


People also ask

Why getText is deprecated?

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.

What is the return type of getText () method?

it actually returns Editable and not CharSequence but you can store it in a String variable by calling toString() on it.


2 Answers

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.

like image 64
MByD Avatar answered Sep 23 '22 17:09

MByD


Try this :

String myPass=String.valueOf(passwordField.getPassword()); 
like image 44
John Cableur Avatar answered Sep 19 '22 17:09

John Cableur