Introduction
Java is an object-oriented, class-based programming language. Java is a high-level programming language that generally emphasizes minimizing the number of implementation dependencies. Java is also a static and general-purpose programming language. It lets the users utilize the concept of write once and run anywhere (WORA). This means that the compiled code can be run on any system having the Java Virtual Compiler (JVM). The Java Virtual Machine or JVM is used to convert the code into a machine-understandable byte code. Now that we know a little about Java, let us discuss methods for adding a NewLine Character to a String in Java.

In Java, adding a new line is as easy as adding "n", "r", or "rn" to the end of our string.
There are five methods to create a new line in Java:
- Platform-dependent newline character
- getProperty() method
- lineSeperator() method
- %n newline character
- out.println() method
1. Platform Dependent New Line Character
Each operating system has a unique character specified to create a new line. For instance, in Windows, we use “\n” to declare a new line. Since every operating system has a specific character for creating a new line, this code is platform dependent and may not work for other platforms.
Implementation in Java
Output

Adding Newline Characters in a String
To show the start of the new line, OS(operating system) provides special characters. On a Linux machine, we can use Line feed(\n) to denote the new line. But in Windows, we can use Carriage return and Line feed(\r\n), it is also known as CRLF.
Using CRLF Line-Breaks
As we discussed before, we can use CRLF to add new line characters in a string. For example, we want to write two lines, one after the other. So, on Windows, we can do this by \r\n.
String str1 = "Hello, Mr. Ninja";
String str2 = "How are you?";
String introOfNinja = str1 + "\r\n" + str2;
On Linux, we can write it as
String introOfNinja = str1 + "\n" + str2;
These methods are platform-dependent, that why we can go with other methods which are platform-independent.
Using Platform Independent Line Separators
When you want your code should be platform-independent, then you can use line separators. Let us consider the above example,
String str1 = "Hello, Mr. Ninja";
String str2 = "How are you?";
String introOfNinja = str1 + System.lineSeparator() + str2;
The System.lineSeparator() method returns the system-dependent line separator. By appending it between the strings, you can achieve line breaks that are appropriate for the current operating system.
Using Platform Independent Newline Characters
Line separators, while ensuring platform independence, necessitate string concatenation. When we employ methods like System.out.printf or String.format, the platform-independent newline character, %n, can be directly utilized within the string. For example,
String introOfNinja="Hello, Mr. Ninja%nHow are you?";
Adding Newline Characters in an HTML Page
To add newline characters in an HTML page, you can use HTML tags or entities to represent line breaks. We can use a break tag(<br>), unicode characters carriage return("
") and line feed("
"). While these characters can be used, their behavior may not be consistent across all platforms. Therefore, it is recommended to use the <br> tag for line breaks as it provides more reliable and consistent results.
HTML Break Tag
We can use a break tag(<br>) to give line breaks. For example, there is a paragraph tag(<p>),
<p>Hello, Ninjas<br>How are you?</p>
<br> tag works with all the elements in HTML. But we cannot use it with <textarea> tag.
Newline Character
For <textarea> or <pre> tag, we can use the \n to give a break in the line. For example,
<textarea>Hello Ninjas,\nWelcome to our platform.</textarea>
Unicode Characters
We can also use Unicode characters or to give a line break. For example,
<p>This is the first line. This is the second line.</p>
Here, the HTML entities or can be used to represent newline characters. represents a line feed (LF) character, while represents a carriage return (CR) character.
Also see, Nmap commands