Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should we use console class?

I was reading about Console class, and in the very first line, it was written

New to Java 6 and when we are running Java SE 6 from command line, then we are typically using console class object

So, which means we are implicitly using console class through the command line ??

Then, I started looking for more detail about Console class and I found Input from console class in java and Console link. So, concluded some points

  1. Console class are only usable outside the IDE using System.console().readLine();
  2. Console class reads a password or passphrase from the console with echoing disabled using readPassword()

Although, we had Scanner class and BufferedReader class to read the input from console and that was added earlier than Java 5. So, only due to security reason Console class was added in Java 6 ? or are there any other advantage(s) to use this class.

Could anyone share more details about Console class ?

like image 766
Ravi Avatar asked Dec 26 '12 11:12

Ravi


People also ask

What is console class why it is used?

Console class provides methods to access the character-based console device, if any, associated with the current Java virtual machine.

What is the use of console?

A console traditionally refers to a computer terminal where a user may input commands and view output such as the results of inputted commands or status messages from the computer. The console is often connected to a remote computer or computer system that is controlled from the console.

Why do we use console in Java?

Java Console is a simple debugging aid that redirects any System. out and System. err to the console window. It is available for applets running with Java Plug-in and applications running with Java Web Start.

Why do we use console in C#?

In C#, the Console class is used to represent the standard input, output, and error streams for the console applications.


3 Answers

The Console class reads directly from the process console (usually /dev/console in Unix systems). The console differs from System.in in that it cannot be redirected when a command is launched. It is also used to read passwords because reading from the console you can control whether or not you echo the chars being typed.

To clarify more on this class, read about console and standard input in Unix systems (this is typically a Unix thing and I'm not really sure how it maps to Windows systems).

Finally, Scanner can read from any input: a file, an stream, or the console itself so it's different from Console.

like image 150
izaera Avatar answered Oct 13 '22 09:10

izaera


The Console class tries to implement a platform independent way to handle with console input. All OS has a console in any way, but they are quiet diferent in implementation. So Console class gives you a Java platfrom independent runtime class to access things like password input, etc.

like image 38
PeterMmm Avatar answered Oct 13 '22 09:10

PeterMmm


Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

How To Get Input From Console Class In Java?

like image 40
UVM Avatar answered Oct 13 '22 09:10

UVM