Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

System.out.println: practical reference

System.out.println: practical reference: PrintStream.println writes the selected value using the matching overload, then writes the platform line separator; Object values are converted through String.valueOf.

Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime

Syntax

System.out.println

PrintStream.println writes the selected value using the matching overload, then writes the platform line separator; Object values are converted through String.valueOf.

Use println for human-readable line-oriented console or diagnostic output where the PrintStream charset and destination are understood.

PrintStream methods normally record I/O failure in the stream error flag instead of throwing IOException. println is not structured serialization, and concurrent calls can interleave with other output.

A null String or Object is printed as the four characters null. Calling println() with no argument writes only the line separator, whose bytes are platform-dependent.

The example exercises the documented element in a small deterministic program and compares exact standard output.

Examples

public class Main {
    public static void main(String[] args) throws Exception {
        var bytes = new java.io.ByteArrayOutputStream();
        try (var stream = new java.io.PrintStream(bytes, true, java.nio.charset.StandardCharsets.UTF_8)) {
            stream.println("Java");
            stream.println(21);
        }
        String text = bytes.toString(java.nio.charset.StandardCharsets.UTF_8);
        System.out.print(text.replace(System.lineSeparator(), "|"));
    }
}

Result

Java|21|

The example exercises the documented element in a small deterministic program and compares exact standard output.

Sources