Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

Java System.out and System.err

Reference for System.out and System.err: println, print, printf, format, flush, checkError, redirection, buffering and why err is separate from out.

System.out is the standard output stream of a Java program, a PrintStream that normally writes to the terminal. System.err is a second PrintStream for error messages. Splitting them lets users redirect normal output to a file or another program while still seeing diagnostics on screen.

PrintStream never throws IOException. If writing fails, it quietly sets an internal flag that you can read with checkError().

Java
System.out.println("Hello");
System.out.print("no newline");
System.out.printf("%s is %d years old%n", "Ana", 31);
System.err.println("something went wrong");

Try it

Java print, println and printf

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Redirecting and checking errors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public static final PrintStream out;
public static final PrintStream err;

Both fields are final in the declaration but can be replaced with System.setOut(PrintStream) and System.setErr(PrintStream).

Method Description
println() Ends the current line.
println(x) Prints x followed by a line separator. Overloads exist for all primitives, char[], String and Object (which uses String.valueOf).
print(x) Same without the separator.
printf(String format, Object... args) Formatted output, same as format. Uses the default locale unless you pass a Locale first.
format(...) Identical to printf.
write(int), write(byte[], int, int) Writes raw bytes.
append(CharSequence) Writes text and returns the stream so calls can be chained.
flush() Pushes buffered bytes out.
checkError() Flushes, then returns true if any write ever failed.
close() Closes the stream. Avoid for the standard streams.

Format specifiers: %d integer, %f floating point, %e scientific, %s string (calls toString), %c char, %b boolean, %x hex, %o octal, %n platform newline, %% a literal percent sign. Flags include - (left-justify), 0 (zero-pad), , (grouping) and + (always show sign).

Usage notes

  • println(char[]) prints the characters, while println(Object) of any other array prints a type tag with a hash. Use Arrays.toString() for readable arrays.
  • Printing null: println((String) null) prints null. println(null) does not compile because the call is ambiguous between char[] and String.
  • printf throws unchecked exceptions for bad formats: MissingFormatArgumentException, UnknownFormatConversionException, IllegalFormatConversionException (for example %d with a double).
  • Locale matters. %,.2f prints 1,234.50 in en_US but 1.234,50 in German locales. Pass Locale.ROOT or Locale.US for reproducible output.
  • Buffering. System.out is buffered internally and flushed on every println when attached to a terminal. For millions of lines, wrap it in a PrintWriter or BufferedWriter for speed, and flush at the end.
  • Thread safety. Individual println calls are synchronized, so lines from different threads do not interleave mid-call, but two separate print calls can be interleaved.
  • Not a logger. System.out has no levels, timestamps or configuration; use System.Logger or a logging framework in real applications.
  • Encoding. Output uses the platform or stdout.encoding charset (Java 19+ property); on terminals that cannot show a character you may see ?.