Lesson · Console, Arguments & the Runtime Environment
Standard streams: System.in, out and err
9 min readStandard Streams
Three byte streams behind three famous names: out and err carry text to the console, in carries bytes and nothing more. The model that turns console programs into Unix citizens.
Out, err, in
System.out is a PrintStream printing to standard output: your program's real results. System.err is another PrintStream for diagnostics and errors, so that java MyProgram > results.txt keeps output in the file while errors still reach the console. System.in is a plain InputStream reading raw bytes from standard input: no text handling, no buffering promises, no end-of-line awareness.
The design is a decades-old Unix convention wearing Java names, and it is why piping works: java Sum < numbers.txt and java Sum | grep treat your program like any other tool. A program that is polite on the command line writes results to out and everything else to err, and reads its input from in or its arguments, never prompting on stdout alone.
Wrapping for convenience
Nobody reads System.in directly. The idiom layers wrapped readers: new BufferedReader(new InputStreamReader(System.in)) turns bytes into characters with the default charset, then into lines with readLine. Since Java 5, Scanner sc = new Scanner(System.in) offers nextInt, nextLine and friends with parsing built in; since Java 6, Console c = System.console() covers passwords and terminal detection.
The decorator stack is the design lesson: InputStream gives bytes, InputStreamReader adds decoding, BufferedReader adds line reading and buffering, Scanner adds token parsing. Each layer is replaceable, which is why tests substitute a StringReader at the bottom and the whole pipe changes source without one line of calling code moving.
The classic mixing bugs
Mixing nextInt() with nextLine() on one Scanner burns everyone once: nextInt consumes just the number and leaves the newline, so the following nextLine returns an empty string immediately. The fix is to call scanner.nextLine() once after reading the number, or read every line as a string and parse deliberately.
Streams and buffering produce the other classics: System.out.print without flush may appear out of order against System.err (different buffers, no guarantee of interleaving), and reading a byte then closing prematurely loses the rest. PrintStream flushes on println (line-buffered when connected to a console) but a redirected file may buffer more: call flush or use println when ordering matters.
Streams in the wider system
The three streams are inheritable: a child process launched with ProcessBuilder receives handles to pipes the parent can write and read. That is how one program feeds another, and it is also why redirecting a program's output changes nothing in its code: the stream is an abstraction over the terminal, a file or another process's input pipe alike.
The practical position: treat out as data, err as messages, in as bytes needing wrapping, and never assume a human is watching. Programs designed that way work in terminals, in scripts and in CI with no changes, which for console tools is the whole ballgame.
out and err are separate channels with separate buffers
Redirecting stdout (java App > data.txt) does not silence errors: System.err still reaches the terminal. And because the two streams buffer independently, output interleaving is not guaranteed. When exact ordering of messages and data matters, flush deliberately, or send both to the same stream.
Interview angles
Why two output streams?+
So programs can separate data from diagnostics: stdout can be piped or redirected while stderr still reaches a human or log. printStackTrace targets System.err by default for exactly this reason; script writers depend on the split.
What is System.in, precisely?+
A raw InputStream over standard input: bytes only. Text reading requires wrapping it (InputStreamReader for the charset, BufferedReader for lines, Scanner for tokens). Closing System.in is rarely right; closing wrappers around it closes it too.
Why is System.console() preferred for passwords?+
readPassword turns off echo at the terminal, so the secret is not displayed, and avoids leaving plaintext in the process's stdout buffers. It returns null when no interactive console exists (IDE runs, CI), so callers still need a fallback path.