Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

System.lineSeparator() — system-dependent line separator

System.lineSeparator() returns the system-dependent line separator. Use it when building text with native line endings, but not instead of a separator required by a data format.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

public static String lineSeparator()
System.lineSeparator()

System.lineSeparator() is a static, no-argument method returning a String. The platform default is "\n" (LF) on UNIX systems and "\r\n" (CRLF) on Microsoft Windows. In OpenJDK 21, a JVM startup option can set a different initial value for line.separator.

The result is always the same: it matches the initial value of the line.separator system property. Changing that property later does not change the method's result; this is not a live lookup through System.getProperty("line.separator").

Use it to assemble text requiring native line endings. If a format specifically requires LF or CRLF, write that required separator instead of selecting one according to the system.

The example joins two text fragments with the separator and prints a comparison result. This makes the expected output true independent of the system, although the separator itself varies.

Examples

public class Main {
    public static void main(String[] args) {
        String separator = System.lineSeparator();
        String text = "first" + separator + "second";
        System.out.print(text.equals("first" + separator + "second"));
    }
}

Result

true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources