Java Certifications
www.javacertifications.com
 home  scjp1.5  scjp1.6  scwcd 1.5  scjp 1.5 questions  scjp 1.6 questions  scwcd 1.5 questions  scjp 1.5 success kit  scjp 1.6 success kit  
  JavaCertifications.net provides java certfication tutorials and mock questions.  


Bookmark and Share |||     Share   
805 scjp 1.6 questions with explanations ||| 788 scjp 1.5 questions with explanations ||| 650 SCWCD 5.0 questions with explanations

Tutorials


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.
scjp 1.5 | scjp 1.6 | scwcd 1.5
Java Certifications
www.javacertifications.net