Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

String formatting and generating text output often comes up during programming. In many cases, there is a need to add a new line to a string to format the output.

Let’s discuss how to use newline characters.

Further reading:

Checking for Empty or Blank Strings in Java

Check out some simple ways in Java to test if a string is blank or empty.

Check If a String Contains a Substring

Explore various ways to search for a substring in a String with performance benchmarks

2. Adding Newline Characters in a String

Operating systems have special characters denoting the start of a new line. For example, in Linux a new line is denoted by “\n”, also called a Line Feed. In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF.

Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

2.1. Using CRLF Line-Breaks

For this example, we want to create a paragraph using two lines of text. Specifically, we want line2 to appear in a new line after line1.

For a Unix/Linux/New Mac-based OS we can use “\n”:

String line1 = "Humpty Dumpty sat on a wall.";
String line2 = "Humpty Dumpty had a great fall.";
String rhyme = line1 + "\n" + line2;

If we are on a Windows-based OS, we can use “\r\n”:

rhyme = line1 + "\r\n" + line2;

For an old Mac-based OS, we can use “\r”:

rhyme = line1 + "\r" + line2;

We’ve demonstrated three methods of adding a new line, but unfortunately, they’re platform dependent.

2.2. Using Platform Independent Line Separators

We can use system defined constants when we want our code to be platform independent.

For example, using System.lineSeparator() for giving a line separator:

rhyme = line1 + System.lineSeparator() + line2;

Or we could also use System.getProperty(“line.separator”):

rhyme = line1 + System.getProperty("line.separator") + line2;

2.3. Using Platform Independent Newline Characters

Although line separators provide platform independence, they force us to concatenate our strings.

If we are using something like System.out.printf or String.format, then the platform independent newline character, %n, can be used directly within a string:

rhyme = "Humpty Dumpty sat on a wall.%nHumpty Dumpty had a great fall.";

This is the same as including System.lineSeparator() within our string, but we don’t need to divide the string into multiple parts.

3. Adding Newline Characters in an HTML Page

Suppose we are creating a string that is part of an HTML page. In that case, we can add an HTML break tag <br>.

We can also use Unicode characters “& #13;” (Carriage Return) and “& #10;” (Line Feed). Although these characters work, they don’t work exactly like we might expect them to across all platforms. Instead, it is better to use <br> for line breaks.

Additionally, we can use “\n” in some HTML elements to break a line.

Overall, these are the three methods of breaking a line in HTML. We can decide which one to use depending on the HTML tag we are using.

3.1. HTML Break Tag

We can use HTML break tag <br> to break a line:

rhyme = line1 + "<br>" + line2;

The <br> tag for breaking a line works in almost all HTML elements like <body>, <p>, <pre>, etc. However, note that it does not work in the <textarea> tag.

3.2. Newline Character

We can use ‘\n’ to break a line if text is enclosed in <pre> or <textarea> tag:

rhyme = line1 + "\n" + line2;

3.3. Unicode Characters

Finally, we can use Unicode characters “& #13;” (Carriage Return) and “& #10;” (Line Feed) to break a line. For example, in the <textarea> tag we can use either of these:

rhyme = line1 + "
" + line2;
rhyme = line1 + "
" + line2;

For the <pre> tag, both of the lines below will work:

rhyme = line1 + "
" + line2;
rhyme = line1 + "

" + line2;

4. The Difference Between \n and \r

\r and \n are characters denoted with ASCII values of 13 (CR) and 10 (LF), respectively. They both represent a break between two lines, but operating systems use them differently. 

On Windows, a sequence of two characters is used to start a new line, CR immediately followed by LF. Conversely, on Unix-like systems, only LF is used.

When writing Java applications, we must pay attention to the line break characters we use because the applications will behave differently depending on the operating system they will run on.

The safest and most cross-compatible option is to use System.lineSeparator(). This way, we won’t have to take the operating system into account.

5. Conclusion

In this article, we discussed how to add newline characters to a string in Java.

We also saw how to write platform independent code for a new line using System.lineSeparator() and System.getProperty(“line.separator”).

Finally, we wrapped up with how to add a new line in case we are generating an HTML page.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW