The answer is simple. Here is a pragmatic approach explaining 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[] , while getText() returns the password as plain text, i.e. in the form of a String .
However, if you print like this,
System.out.println(new String(jt.getPassword()));
This is a lot equal to getText() in JPasswordField . However, this does not mean that getPassword() uses getText() internally and then converts it to a char array.
The getPassword() method uses a non-string API, i.e. Segment . However, the Segment again immutable, but the getPassword() method brings an array of char from the Segment and returns it.
However, since String is immutable and char[] not, a char[] is considered quite safe since it can be erased.
source share