|
SCJP : java.io.Console
If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.
Read and write operations are synchronized to guarantee the atomic completion of critical operations; therefore invoking methods readLine(), readPassword(), format(), printf() as well as the read, format and write operations on the objects returned by reader() and writer() may block in multithreaded scenarios.
Console has readPassword() method that disables console echo and returns a char array. You can't see the password in console.
Sample Questions
What is the output?
import java.io.Console;
public class Test {
public static void main(String... args) {
Console con = System.console();
boolean auth = false;
if (con != null)
{
int count = 0;
do
{
String uname = con.readLine(null);
char[] pwd = con.readPassword("Enter %s's password: ", uname);
con.writer().write("\n\n");
} while (!auth && ++count < 3);
}
}
}
The output:
NullPointerException
passing a null argument to any method in Console class will cause a NullPointerException to be thrown.
| |