11

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 ?

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • 2
    You are not "implicitly using" Console. From Java6, it is just the easiest and preferred way to deal with keyboard input, so you want to use it instead of the older methods (which all still work). – Thilo Dec 26 '12 at 11:14
  • 1
    @Thilo, if you are asking me, whether i should prefer older one of newer. Then, i will prefer older one. Since, for me there is not much difference between them (i think). – Ravi Dec 26 '12 at 11:19

3 Answers3

4

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.

Ravi
  • 30,829
  • 42
  • 119
  • 173
xxxxxxxxx
  • 999
  • 5
  • 11
2

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.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
1

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?

Community
  • 1
  • 1
UVM
  • 9,776
  • 6
  • 41
  • 66
  • 2
    you just copied all contents. Nevertheless, but, if briefly explain why `console` class added in `java 6` in your own word. Then, it will be easy to understand. – Ravi Dec 26 '12 at 11:24
  • It is clearly mentioned in simple language here. That is why I copied from the spec. – UVM Dec 26 '12 at 11:26
  • 1
    agree, it would be easy word, but not for me. So, i will appreciate, if you will explain some feature and advantage of `console` class using few example/scenario or some reference. – Ravi Dec 26 '12 at 11:29
  • what is new in your answer ??. please read my question, you will find same link in my question. – Ravi Dec 26 '12 at 11:38