Home » Java programming language

Java System class console() method with example

System class console() method: Here, we are going to learn about the console() method of System class with its syntax and example.
Submitted by Preeti Jain, on September 14, 2019

System class console() method

  • console() method is available in java.lang package.
  • console() method is used to return the console object which is uniquely associated with the current JVM(Java Virtual Machine) if exists.
  • console() method is static, it is accessible with the class name too.
  • console() method does not throw any exception.

Syntax:

    public static Console console();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is Console, it returns the Console object if exists else it returns null when none of the System console exists.

Example:

// Java program to demonstrate the example of 
// console() method of System Class.

import java.io.*;

public class ConsoleMethod {
    public static void main(String[] args) {
        // Creating an object of Console
        Console con = System.console();

        if (con == null)
            // Display when console is null
            System.out.println("Null Console");
        else {
            String str = con.readLine("%s", "Java Programming");
            // Display when console is not null
            System.out.println("The value of str is :" + str);
        }
    }
}

Output

E:\Programs>javac ConsoleMethod.java
E:\Programs>java ConsoleMethod
Null Console


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.