The answer has already been given, but I'd like to share an issue that I discovered lately with Java standard libraries. While they take great care now of replacing password strings with char[] everywhere (which of course is a good thing), other security-critical data seems to be overlooked when it comes to clearing it from memory.
I'm thinking of e.g. the PrivateKey class. Consider a scenario where you would load a private RSA key from a PKCS#12 file, using it to perform some operation. Now in this case, sniffing the password alone wouldn't help you much as long as physical access to the key file is properly restricted. As an attacker, you would be much better off if you obtained the key directly instead of the password. The desired information can be leaked manifold, core dumps, a debugger session or swap files are just some examples.
And as it turns out, there is nothing that lets you clear the private information of a PrivateKey from memory, because there's no API that lets you wipe the bytes that form the corresponding information.
This is a bad situation, as this paper describes how this circumstance could be potentially exploited.
The OpenSSL library for example overwrites critical memory sections before private keys are freed. Since Java is garbage-collected, we would need explicit methods to wipe and invalidate private information for Java keys, which are to be applied immediately after using the key.
char[]being more secure except maybe the most amateurish of threats. – Viruzzo Jan 16 '12 at 14:23getText()method ofJPasswordFieldis deprecated in favour ofgetPassword()"for security reasons". – dogbane Jan 16 '12 at 14:32char[]andStringobjects are going to be passed by reference; individualcharvalues will not be. It's when objects are passed by value, not reference, that multiple copies are created. Generally, this is not an issue, as such objects are usually designed to be small. – Zenexer Jun 15 '13 at 11:25