The answer is simple. Here is a pragmatic approach that explains the difference between getPassword()
and getText()
JPasswordField jt=new JPasswordField("I am a password");
System.out.println("The text is "+jt.getText());
System.out.println("The password is "+jt.getPassword());
Output
I am a password
[C@1e4a47e
The getPassword()
method returns the password as char[]
whereas the getText()
returns the password as plain text i.e. in the form of String
.
However, if you do print like this,
System.out.println(new String(jt.getPassword()));
This is much equal to getText()
in JPasswordField
. However this does not mean that getPassword()
uses the getText()
internally and then convert it into char
array.
The getPassword()
method uses the non-string API i.e. the Segment
. However, Segment
is again immutable, but the getPassword()
method brings the char array from the Segment
and returns it.
However as String
is immutable and char[]
is not, a char[]
is considered quite secure because it can be wiped out.