What is n in Java?
1. Introduction
This is an in-depth article related to the n in Java. \n is used for inserting a new line in the text. The control is passed to the next line and text is printed on the new line.
2. \n in Java
Adding a new line in java is done by including “\n” at the end of the string.
2.1 Prerequisites
Java 7 or 8 is required on the Linux, windows, or mac operating system. Maven 3.6.1 is required for building the spring and hibernate application.
2.2 Download
You can download Java 8 can be downloaded from the Oracle website.
2.3 Setup
You can set the environment variables for JAVA_HOME and PATH. They can be set as shown below:
Setup
JAVA_HOME="/desktop/jdk1.8.0_73" export JAVA_HOME PATH=$JAVA_HOME/bin:$PATH export PATH
The environment variables for maven are set as below:
Maven Environment
JAVA_HOME=”/jboss/jdk1.8.0_73″ export M2_HOME=/users/bhagvan.kommadi/Desktop/apache-maven-3.6.1 export M2=$M2_HOME/bin export PATH=$M2:$PATH
2.4 Using CRLF Line-Breaks
Carriage Return and Line Feed Breaks are achieved by using “\r\n”. In Mac Os, we can use “\n”. Below is the example which shows CRLF line breaks for a new line.
CRLF Line Break
public class LineBreakExample { public static void main(String[] args) { String line1 = "Line1."; String line2 = "Line2."; String line3 = line1 + "\n" + line2; System.out.println(line3); } }
The output of the code when compiled and executed is shown below:
CRLF Line Break Output
(base) apples-MacBook-Air:n_line_new bhagvan.kommadi$ javac LineBreakExample.java (base) apples-MacBook-Air:n_line_new bhagvan.kommadi$ java LineBreakExample Line1. Line2.
2.5 System.lineSeparator()
System-defined constants can be used to make the code platform independent. Below is the example which shows the usage of line separator using System.lineSeparator().
Line Separator
public class LineSeparator { public static void main(String[] args) { String line1 = "Line1."; String line2 = "Line2."; String line3 = line1 + System.lineSeparator() + line2; System.out.println(line3); } }
The output of the executed command is shown below.
Line Separator Output
(base) apples-MacBook-Air:n_line_new bhagvan.kommadi$ javac LineSeparator.java (base) apples-MacBook-Air:n_line_new bhagvan.kommadi$ java LineSeparator Line1. Line2.
2.6 System.getProperty(“line.separator”)
Similarly, System.getProperty(“line.separator”) can be used for achieving the same newline printing.
Line Separator Property
public class LineSeparatorExample { public static void main(String[] args) { String line1 = "Line1."; String line2 = "Line2."; String line3 = line1 + System.getProperty("line.separator") + line2; System.out.println(line3); } }
2.7 %n
Platform independent line separator can also be used with System.out.println like %n. Below is the example for the usage of %n.
New Line Character
public class NewLineCharacterExample { public static void main(String[] args) { String line1 = "Line1."; String line2 = "Line2."; String line3 = line1 + "%n" + line2; System.out.println(line3); } }
2.8 The Difference Between \n and \r
Now let us look at the difference between CRLF line break characters n and \r in Java. \n stands for an ASCII value of 10 (Line Feed) and \r stands for an ASCII value of 13 (Carriage Return). Both are used for creating a break between two lines. On Windows, Carriage Return and Line Feed are used. Line Feed alone is used in Unix.
3. Download the Source Code
You can download the full source code of this example here: What is \n in Javal