Core Java

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

Download
You can download the full source code of this example here: What is \n in Javal

Bhagvan Kommadi

Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button