In Swing, the password field has a getPassword()
(returns char[]
) method instead of the usual getText()
(returns String
)
I don't think this is a valid suggestion, but, I can at least guess at the reason.
I think the motivation is wanting to make sure that you can erase all trace of the password in memory promptly and with certainty after it is used. With a char[]
you could overwrite each element of the array with a blank or something for sure. You can't edit the internal value of a String
that way.
But that alone isn't a good answer; why not just make sure a reference to the char[]
or String
doesn't escape? Then there's no security issue. But the thing is that String
objects can be intern()
ed in theory and kept alive inside the constant pool. I suppose using char[]
forbids this possibility.