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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.