805 OCPJP 6 Questions 788 SCJP 5 questions 650 OCPJWCD 5 questions 600 OCAJP 7 questions 610 OCPJP 7 questions 510 Upgrade to OCPJP 7 questions 900 Java J2EE Qns


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;
		      String uname = con.readLine(null);
		      char[] pwd = con.readPassword("Enter %s's password: ", uname);
		    } while (!auth && ++count < 3);

The output:

passing a null argument to any method in Console class will cause a NullPointerException to be thrown.
scjp 1.5 | scjp 1.6 | scwcd 1.5
Java Certifications