Adding a Newline Character To a String In Java

April 28, 2022
Remote Java Project


When Java developers work with string formatting and generating text output that would be presented on the screen, programmers need to add a new line to a string to format the output. This formatting is very important as it makes the output more readable and coherent for the users. It can be done in Java by adding newline characters to a string.

What is a newline character?

newline character, also known as the end of line (EOL), line break or line separator is a control character used to represent the end of a line and the beginning of a new one, separating both of them.

In various programming languages including Java, there are multiple ways to add a new line in a string. All these methods are slightly different from each other based on their conditions and limitations. We will be discussing some prominent ways of adding a newline character to a string in Java.

new java job roles

Platform dependent process:

· Using an escape sequence:

There is a list of escape sequences offered in Java. An escape sequence is a group of two or more characters, that often begins with an escape character() that tells the compiler to perform a function or command.

Two of these escape sequences are used to add a new line in Java:

Escape Sequence Description
n Inserts a new line in the text at this point.
r Inserts a carriage return in the text at this point.

They both perform the same task depending on the platform you are using. Most of the popular operating systems that support Java have distinct escape sequences to denote the end of the line. In Linux and new Mac operating systems, the end line is denoted by “n”, also called line feed. In old Mac operating systems, developers just need to use “r”, known as carriage return. Lastly, In Windows operating systems, developers need to use both of these escape sequences together, “rn”.

See the examples below, here escape sequences are used to output a paragraph using two lines of text. The second is supposed to appear in a new line after the first one.

For a Linux or MacOS following code will work:

1. String line01 = "This is the first string.";
2. String line02 = "This is the second string.";
3. String newString = line01 + "n" + line02;

If we have to do this on a Windows operating system, we would have to use “rn” so the last line of code would look like this:

String newString = line01 + "rn" + line02;

While working on an old Mac-based operating system, the last line of code is supposed to be like this:

String newString = line01 + "r " + line02;


This is the first string.

This is the second string.

The escape sequences work perfectly fine but since they are bound with the platforms it is recommended not to use them to add a new line character to a string in Java. This is because when using multiple platforms, and operating systems, using platform-independent line breaks is the most risk-free option because it is cross-compatible.

Platform independent process:

· Using System.lineSeparator() method:

System.lineSeparator() method can be used to separate two lines in java. It is a platform-independent line break which means that it will work just fine irrespective of any operating system. You can also use System.getProperty(“line.separator”) to put new line character in String.

See the code below demonstrating the System.lineSeparator() method to add a newline character to a string:

1. public class NewLineCharacter {
2.     public static void main(String[] args) {
3.        System.out.print("This is Line 1");
4.        System.out.print(System.lineSeparator());
5.        System.out.print("This is Line 2");
6.        // it can also be written like this,
7.        System.out.print("This is Line 3" + System.lineSeparator() + "This is Line 4");
8.    }
9. }

Output :

This is Line 1

This is Line 2

This is Line 3

This is Line 4

Although line separator provides the platform independence as well as gets the job done, it is still a separator, not a newline character because it restricts the coder to concatenate the strings. Developers can either use it separately as mentioned in the first example or they have to use it with concatenation.

· Using a newline character:

There is also a way to add a Newline Characters to a string without concatenating multiple strings. A newline character “%n” can be used in a string to break it up and add a new line in java. It is the same as including System.lineSeparator(), but we do not need to divide the string into multiple parts. See this example:

newString = "This is the first string.%nThis is the second string.";


This is the first string.

This is the second string.

Now, the question is why we don’t just use it straight away skipping all the other methods. It is a small and simple newline character but there is a small problem. You can use “%n” only when “System.out.printf” or “String.format”, statements are used to output the string. If you have been using System.out.print to print a string then, using “%n” newline character can lead to repeating errors in your code.

Writing a newline character using BufferedWriter:

We use BufferedWriter class to write text into a file. Similarly, it can be used to write text into a string. Now, just like text, a newline character can also be added to the string. The newline () method is used for that. It is used as a write separator in a buffered writer stream.

Following is the syntax of the newline() method:

1. public void newLine()

2.            throws IOException

The newline() method does not require any parameters and doesn’t return any value. It throws an IOException if an I/O error occurs or most commonly if the file is not found.

See this code below using newline() to add a new line into a string:

1. import*;
2. public class newLineExample {
3.    public static void main(String[] args)
4.        throws IOException
5.    {
6.          // Creating the string Writer
7.          StringWriter stringWrite = new StringWriter(); 
8.          // Converting stringWriter to bufferedWriter        
9.          BufferedWriter buffWriter = new BufferedWriter(stringWriter);
10.         buffWriter.write("This is the first string.");
11.         buffWriter.newLine(); // Using newLine() method
12.         buffWriter.write("This is the second string.");
13.         buffWriter.flush();
14.         System.out.println(stringWriter.getBuffer());
15.    }
16. }


This is the first string.

This is the second string.


In this article, we discussed several ways to add a new line into a string in Java. We also covered how your Java code could be platform-dependent or independent based on using different newline characters as well as explored a way to use the BufferedWriter class to add a new line in Java.

See Also: Switch Case Statement In Java With Example

Inadequate knowledge about Java new line can generate various errors in your program. Due to a variety of ways to add a new line. you should be aware of all these methods as you may come across any of them during your projects. This small yet crucial understanding of Java is what makes a developer stand out.

new Java jobs



Shaharyar Lalani is a developer with a strong interest in business analysis, project management, and UX design. He writes and teaches extensively on themes current in the world of web and app development, especially in Java technology.

Candidate signup

Create a free profile and find your next great opportunity.


Employer signup

Sign up and find a perfect match for your team.


How it works

Xperti vets skilled professionals with its unique talent-matching process.


Join our community

Connect and engage with technology enthusiasts.