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().
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
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.
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
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, whileprintln(Object)of any other array prints a type tag with a hash. UseArrays.toString()for readable arrays.- Printing
null:println((String) null)printsnull.println(null)does not compile because the call is ambiguous betweenchar[]andString. printfthrows unchecked exceptions for bad formats:MissingFormatArgumentException,UnknownFormatConversionException,IllegalFormatConversionException(for example%dwith adouble).- Locale matters.
%,.2fprints1,234.50inen_USbut1.234,50in German locales. PassLocale.ROOTorLocale.USfor reproducible output. - Buffering.
System.outis buffered internally and flushed on everyprintlnwhen attached to a terminal. For millions of lines, wrap it in aPrintWriterorBufferedWriterfor speed, and flush at the end. - Thread safety. Individual
printlncalls are synchronized, so lines from different threads do not interleave mid-call, but two separateprintcalls can be interleaved. - Not a logger.
System.outhas no levels, timestamps or configuration; useSystem.Loggeror a logging framework in real applications. - Encoding. Output uses the platform or
stdout.encodingcharset (Java 19+ property); on terminals that cannot show a character you may see?.