Generating text-based output and string formatting is a common practice for programmers and software developers. In almost all situations, while creating any string as output, programmers usually need to put the statements in multiple lines. Without proper formatting in the program's output, the readability of the code will decline. That is why this tutorial will highlight all the different means of creating new lines in Java.
What is a Newline Character?
A Newline character is a particular type of character literal or an escape sequence in a Java program, representing the end of a line and the beginning of a line. In programming languages like C, C++, Java, etc., programmers use the '\n' to make a new line in the string formatting. Sometimes, this new line character literal, called line feed, line break, line separator, or EOL (End of Line).
This string formatting control character tells the Java compiler where the text will move to a new line or when the next character should start in the new line. In the Windows programming environment, programmers need to use CRLF (Carriage Return Line Feed), which is the combination of '\r\n' both Carriage Return and Line Feed. In the Linux programming environment, programmers need to use '\n' - which is line feed or newline escape sequence.
What are the differences between '\n' and '\r'?
In Java, there are programming situations where both these character literals show the same outcome. But there are some distinct differences between both of these.
- Programmers use \n to move the cursor position to the beginning of a new line. On the other hand, \r takes the cursor's place to the beginning of the same line.
- \n is the line feed (LF) whereas \r is called carriage return (CR)
- The ASCII character code for \n is 10, whereas the character code for \r is 13
- In a Windows system, programmers terminate the lines in text files using CR followed by LF in combination (e.g., CRLF, i.e., '\r\n'). In a Unix system, programmers do the same using line feed (LF) only.
Therefore, Java programmers need to heed attention to the line break characters they use because applications might behave differently based on the OS on which they operate. Therefore, Java introduces the safest and most cross-compatible option - System.lineSeparator(), which we will discuss in the subsequent section.
Adding Newline Characters in a String
Creating a newline character in Java is not that difficult. But there are different ways through which we can use newline characters for string formatting.
1) Using System.lineSeparator()
Java has a built-in line separator lineSeparator() that is a method of the System class. Since the newline separator varies from platform to platform, it is usually required to have a platform-dependent way of using it. Since the new line separator is a common element of string formatting, Java 7 came up with a shortcut method that returns the same result as \n does. Here is a code snippet showing the use –
import java.lang.System.*;
class Main
{
public static void main(String[] args) {
String newlineusinglinesep = System.lineSeparator();
System.out.println("Hey" + newlineusinglinesep + "Karlos");
}
}
2) Using CRLF Line-Breaks
Programmers can combine \r (CR) and \n (LF) together, (called the CRLF - Carriage Return Line Feed) to create a new line within a string. Note that this technique is OS and platform-dependent, i.e., it will work properly on Windows systems only.
import java.lang.System.*;
class Main
{
public static void main(String[] args) {
System.out.println("Hey" + "\r\n" + "Karlos");
}
}
3) Using Platform Dependent Line Separators (\n)
The most commonly used mechanism of creating a newline character is using the '\n' (Line Feed) special character literal. We call it a platform-dependent newline separator because only '\n' works on Unix programming environment and '\r\n' are both combined together to make it functional in Windows-based systems.
import java.lang.System.*;
class Main
{
public static void main(String[] args) {
System.out.println("Hey" + "\n" + "Karlos");
}
}
4) Creating Newline using getProperty() method
Another standard and recommended method of using newline in Java is using the getproperty method and passing the line.separator as a string parameter. Note that this method will return a system-dependent line separator because its value depends on the underlying operating system.
import java.lang.System.*;
class Main
{
public static void main(String[] args) {
String newlineusingGetProp = System.getProperty("line.separator");
System.out.println("Hey" + newlineusingGetProp + "Karlos");
}
}
5) Creating Newline using %n
This is another plausible means of creating a platform-independent line separator '%n' within Java's printf() method.
import java.lang.System.*;
class Main
{
public static void main(String[] args) {
System.out.printf("Hey %n Karlos");
}
}
7) Creating Newline using System.out.println() method:
In general, if a programmer wishes to have a new line after the string at the end of every statement, he or she can use Java's System.out.println() method. This method automatically puts a new line character at the end of every string passed within it.
import java.lang.System.*;
class Main
{
public static void main(String[] args) {
System.out.println("Hey");
System.out.println("karlos");
}
}
Java Program to Print a Newline Character
Here is a program explanation showing the use of different newline separators in Java.
import java.lang.System.*;
class Main
{
public static void main(String[] args) {
System.out.print("Hey" + "\n" + "Karlos" + "\n");
System.out.println("Hey");
System.out.println("karlos");
String newlineusinglinesep = System.lineSeparator();
System.out.println("Hey" + newlineusinglinesep + "Karlos");
}
}
In this program, we have created a Main class, and within that, we have created the main() where we have used the strings "Hey" + "\n" + "Karlos" + "\n" separated by \n. On the next two lines, we have created two out.println() statements which will automatically use the new line character after the end (here "Hey" & "Karlos").
On the next line String newlineusinglinesep = System.lineSeparator(); will create a string-based variable that will assign the lineSeparator() functionality within it. Then, we have used it within the string as a separator.
We create this by assigning it within a static variable rather than retrieving it from the system every time we require it.
Conclusion
Finally, we can say that the lineSeparator() method is the best way among all of these if you want to insert a line separator anywhere within the string. This is because the lineSeparator() is independent of the platform or operating system environment which makes it flexible.
Every other method is dependent on something. System.out.println() is dependent because it adds a new line at the end of the string. %n String format requires printf() method to make it functional. The getProperty() method needs the parameter which makes it complex. CRLF and '\n' are dependent on platforms. Thus, System.lineSeparator() in Java is the most efficient.