Java Console 4 of 5

Method # 3 readPassword

public char[] readPassword()

Reads a password or passphrase from the console with echoing disabled


A character array containing the password or passphrase read from the console, not including any line-termination characters, or null if an end of stream has been reached.


IOError - If an I/O error occurs.

Very Important method to remember, you can expect a question on this method. ReadPassword is similar to readLine() method except for the fact that readlLine method prints the charecters on the console whereas readPassword method does not.  But Why ?? ( You know the answer right J)

If you carefully observe there is one more difference between readLine method and readPassword method. Have you observed it ?.  (Hint : See the method definitions carefully)

I know you have found it. The other difference is readLine method returns a String whereas readPassword method returns a char array.

Now one more question, why cant readPassword return String ?. This is because String objects will be stored inside a string buffer and java don’t want any malicious hacker (like me J) to get the Strings back from the memory.

Now we’ll write one more code to verify the usage of readPassword method.

Code 3 : Demonstrates the usage of readPassword() method
<<< Previous || Next >>>

ALL about SCJP = SCJP 1.6, mock, certification, scjp 1.5 , scjp questions, scjp books, scjp hand written notes, scjp dumps, scjp trainings, scjp video tutorials, scjp simulators etc.