Java System lineSeparator() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

In Java, System.lineSeparator() returns the line break string of the operating system the program runs on: "\n" (line feed) on Linux and macOS, and "\r\n" (carriage return + line feed) on Windows.

Use it when you build text that should look native on each system, for example a report saved for the user or a file opened in Notepad. It returns the value of the line.separator system property as it was when the JVM started, and println and %n in printf use the same line break.

A static method of the java.lang.System class, added in Java 7 (JDK 1.7). Call it on the class name, as in System.lineSeparator(...). No import is needed, because classes in java.lang are always visible.

Syntax

No arguments; append the result wherever a line should end:

Java
String nl = System.lineSeparator();       // "\n" or "\r\n"
String text = "Line 1" + nl + "Line 2" + nl;
String same = String.format("Line 1%nLine 2%n");
CallWhat it does
System.lineSeparator()Line break of the current platform.
System.getProperty("line.separator")The same value as a property (Java 1.0+); may be changed by code.
String.format("%n")The platform line break inside a format string.
text.split("\\r?\\n")Splits text into lines whatever line breaks it uses.

Added in Java 7. On Java 6 and older, use System.getProperty("line.separator"). Text you read may use any style of line break, regardless of the platform you run on.

Library declarations
Java
// java.lang.System
public static String lineSeparator()

Parameters

ParameterDescription
NonelineSeparator() takes no parameters.

Return Value

Return ValueDescription
StringThe system line separator: "\n" on Unix-like systems such as Linux and macOS, "\r\n" on Windows. Never null.

Compare with Similar Methods

Ways to end a line:

MethodHow it differs
"\n"Always a line feed. Most editors on every system display it fine.
getProperty("line.separator")Same value at startup, but it is a property that code can overwrite.
println() / %nAdd the platform line break for you when printing or formatting.
BufferedWriter.newLine()Writes the platform line break into a file or stream.

Java Examples

Example 1 — Inspect the Line Separator Without Printing It

Printing the separator itself would only show an empty line. These checks give the same answers on Linux, macOS, and Windows.

Java
public class SystemLineSeparatorCheck {
    public static void main(String[] args) {
        String sep = System.lineSeparator();
        System.out.println("Matches line.separator: " + sep.equals(System.getProperty("line.separator")));
        System.out.println("Length is 1 or 2: " + (sep.length() == 1 || sep.length() == 2));
        System.out.println("Ends with \\n: " + sep.endsWith("\n"));
    }
}

How It Works

  1. Same source. lineSeparator() returns the startup value of the line.separator property, and nothing has changed the property, so equals is true.
  2. One or two characters. It is "\n" (length 1) on Linux and macOS or "\r\n" (length 2) on Windows; checking for either keeps the output identical everywhere.
  3. Escapes in strings. In the label, "\\n" prints a backslash and the letter n; in endsWith("\n") it is a real line feed, which both styles end with.

Example 2 — Build Multi-Line Text with the Platform Line Break

A StringBuilder collects the lines, so a saved copy would open correctly in any editor on the current system.

Java
public class SystemLineSeparatorReport {
    public static void main(String[] args) {
        String nl = System.lineSeparator();
        StringBuilder report = new StringBuilder();
        report.append("Fruit report").append(nl);
        report.append("Apples: 4").append(nl);
        report.append("Pears: 7").append(nl);
        System.out.print(report);

        String text = report.toString();
        int breaks = (text.length() - text.replace(nl, "").length()) / nl.length();
        System.out.println("Lines: " + text.split(nl).length);
        System.out.println("Line breaks: " + breaks);
    }
}

How It Works

  1. One separator variable. Reading System.lineSeparator() once into nl keeps every line ending consistent.
  2. print, not println. The report already ends with a line break, so System.out.print(report) avoids an extra blank line.
  3. Count without knowing the platform. Removing every nl and dividing the lost length by nl.length() counts 3 breaks whether each one is 1 or 2 characters; split also finds 3 lines.

Example 3 — %n, and Reading Text with Any Line Break

%n in a format string equals lineSeparator(). Text from elsewhere may use another style, so split it with a pattern that accepts both.

Java
public class SystemLineSeparatorFormat {
    public static void main(String[] args) {
        String viaFormat = String.format("a%nb");
        String viaMethod = "a" + System.lineSeparator() + "b";
        System.out.println("%n equals lineSeparator(): " + viaFormat.equals(viaMethod));

        String fromWindows = "red\r\ngreen\r\nblue";
        String fromLinux = "red\ngreen\nblue";
        System.out.println("Windows text lines: " + fromWindows.split("\r?\n").length);
        System.out.println("Linux text lines: " + fromLinux.split("\r?\n").length);

        String converted = fromLinux.replace("\n", System.lineSeparator());
        System.out.println("Converted keeps 3 lines: " + (converted.split(System.lineSeparator()).length == 3));
    }
}

How It Works

  1. %n is the platform break. String.format replaces %n with System.lineSeparator(), so both strings are identical on every system. In println the text "%n" is not a format, so it prints as is.
  2. Accept both styles. The regular expression \r?\n means "an optional carriage return, then a line feed", so Windows and Linux text both split into 3 lines.
  3. Convert for the local system. Replacing each "\n" with the platform separator gives native line breaks; splitting on that separator again still finds 3 lines.

Frequently Asked Questions

The line break of the current operating system: "\n" on Linux and macOS and "\r\n" on Windows.
"\n" is always a single line feed. lineSeparator() depends on the platform, so on Windows it returns the two characters "\r\n".
It returns the value the property had when the JVM started. If code later changes the property, getProperty returns the new value but lineSeparator() does not change.
Java 7. On older versions use System.getProperty("line.separator").
No. Protocols such as HTTP and SMTP require "\r\n" on every platform, so write those characters explicitly.

Next: load()

Load a native library from an absolute file path.

load() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful