×

Java Console

If there is a character-based console device connected to the active Java virtual machine, it can be accessed using methods provided by the Java.io.Console class. JDK 6 adds the Console class to the java.io API.

To obtain console input, utilise the Java Console class. It offers ways to read text messages and passwords.

The user won't see the password if you read it using the Console class.

Internally, the system console is connected to the java.io.Console class. Since 1.5, the Console class has been available.

Here is a straightforward example of reading text from a console.

String text=System.console().readLine();    
System.out.println("Text is: "+text);    

Important Information

  • If there is a console, it is used to read from and write to it.
  • Because System.in and System.out provide access to the majority of Console's capabilities, it serves mostly as a convenience class. However, when reading strings from the console, its use can make some console interactions simpler.
  • Constructors are not provided by Console. Instead, using System.console() returns a Console object, as demonstrated here:
 static  Console console( )

A reference to the console is returned if one is present. Null is returned if not. In some circumstances, a console won't be accessible. Therefore, no console I/O is possible if null is returned.

  • It offers techniques for reading text and passwords. The user won't see the password if you read it using the Console class. Internally, the system console is connected to the java.io.Console class.

Java Console class declaration

Let's see the declaration for Java.io.Console class:

public final class Console extends Object implements Flushable 

Java Console class methods

MethodDescription
Reader reader()It is utilised to retrieve the console's related reader object.
String readLine()A single line of text from the console is read using this method.
String readLine(String fmt, Object... args)A formatted prompt is given before the console's single line of text is read.
char[] readPassword()It can read passwords that are not visible on the console.
char[] readPassword(String fmt, Object... args)The formatted prompt is given, and after that, the console reads the password that isn't being displayed.
Console format(String fmt, Object... args)A prepared string can be written with it. to the output stream on the terminal.
Console printf(String format, Object... args)It is employed to write a string to the output stream of the console.
PrintWriter writer()To retrieve the PrintWriter, use this. a piece connected to the console.
void flush()The console is flushed using it.

How to obtain the console's object

The static function console() of the System class returns a single instance of the Console class.

public static Console console(){}   

Let's see the code to get the instance of Console class.

Console c1= System .console();  

Program for Java Console

import java . io . Console ;  
class Read_string {    
public static void main ( String args [ ] ) {    
Console c1 = System . console ( ) ;    
System . out . println ( " Enter your name: " ) ;    
String name = c1 . readLine ( ) ;    
System . out . println ( " Hello "+ name ) ;    
}    
}  

Output

Java Console

Program for Java Console for reading password

import java . io . Console ;  
class  {    
public static void main ( String args [ ] ) {    
Console c1 = System . console ( ) ;    
System . out . println ( " Enter password: " ) ;    
char [ ] character = c1 . readPassword ( ) ;    
String password = String . valueOf ( character ) ;
System . out . println ( " Password is: "+ password ) ;    
}    
}  

Output

Java Console

Related Topics

Java file Reader

File Reader: It is used to read the data from files. This class inherits the properties from Input Stream Reader Class. File Reader is for reading characters from the file. Input Stream: Java.io...

4 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Program to Reverse a Number in Java

In order to reverse a number, the digit in the first place must be swapped with the digit in the final position, the second digit with the second-to-last digit, and...

3 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below. The game begins with an integer num, used to create...

2 minutes read.

How to set timer in Java

In this article, you will be very well equipped with the knowledge to set timer in java. The timer in java can be set by using timer class provided by...

3 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

How to Convert Hexadecimal to Decimal in Java

How to Convert Hexadecimal to Decimal in Java There are two methods to convert Hexadecimal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method It is a static method of...

2 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

Types of Events in Java

One of Java's key ideas is the principle of an event. Events in Java are activities that result in a change in the state or behavior of an object. The...

4 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

JIT in Java

What is JIT ? JIT stands for " Just in Time Compiler ". It is called "Just in time" because it is called at the very last moment when the interpretation...

5 minutes read.