1

What is the difference between the "standard streams" (System.out, System.in, System.err) and the "Console" (System.console()) in Java?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Rekha
  • 1,403
  • 3
  • 12
  • 21

2 Answers2

2

Every process (not just Java programs) have three streams: in, out, and error. In Java access to these has been simplified to System.in, System.out, and System.err. These are used for reading from or writing to the command line. For example, if you ever had a command line program that asked for input, which you typed and then pressed enter, that input went to standard in.

There are two out streams (out and err), because they report different things. For example, you might want to save the error output but not care about the generic output a program prints out. Or you might want to suppress the standard output so only errors are printed. Or you have a program with all kinds of problems and you want to suppress the errors so you can see what it's trying to do and not 10 million stack traces.

So, the short answer to your question is that the standard streams are ways of writing to and reading from the console.

Kane
  • 4,047
  • 2
  • 24
  • 33
0

In short, a console is the thing that might capture the streams and display them on a screen. Every program has access to the input/output streams, but not every program has access to a console.

Some programs lose access to the console as their streams are initially (or eventually redirected). Other programs are launched in configurations where they never had access to a console. In the latter case, everything written to System.out might as well have been written to /dev/null.

While this applies to Java, the Console (and the "other side" of the streams) are really operating system concepts.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138