How to Print New Line in Java

Rashmi Patidar Feb 02, 2024
  1. Print a New Line Using the println() Function in Java
  2. Print a New Line Using the Escape Sequence \n Character in Java
  3. Print a New Line Using getProperty() Method in Java
  4. Print a New Line Using the lineSeparator() Method in Java
  5. Print a New Line Using %n Newline Character in Java
How to Print New Line in Java

A new line signifies the end of a line or the start of a new line. It is also known as linebreak, EOL(end Of line), or a line feed character.

In the Java language, there are various ways to print a new line. We can use escape sequence or predefined methods of the System class.

In the below code block, we use the println() function to print a new line. The System class contains various useful methods and fields, and we cannot instantiate this class. The out is the standard output stream object present in the System class. Typically this stream corresponds to display output specified by the host environment or user. println() is a method in the PrintStream class. It prints a String and then terminates the line. It takes a string as a parameter that we want to print on the console output. The print() function prints a string. If the argument is null, then the string null is printed. Otherwise, the string’s characters get converted into bytes according to the platform’s default character encoding.

package new_line;

public class WaysToPrintNewLine {
  public static void main(String[] args) {
    System.out.println("Line1");
    System.out.print("Line2");
  }
}

Below is the output of the above two lines.

Line1
Line2

There are some cases where we want to print a new line within the text in the console output. Using the println() method for such a case would be a redundant task. In such cases, we can use the escape sequence for better code readability. An escape sequence is a character that consists of a backslash \ just before it. Examples of such characters are:

  1. We use \t to insert a tab in the text.
  2. We use \b to insert a backspace in the text.
  3. We use \n to insert a new line in the text.

In the below code, \n escape sequence is used to break inline statements into two different lines.

package new_line;

public class WaysToPrintNewLine {
  public static void main(String[] args) {
    System.out.println("Hi, I am Lee"
        + "\n"
        + "I will help you write the code.");
  }
}

The output of the above code is as below.

Hi, I am Lee I will help you write the code.

In this way, we will use the getProperty() method of the System class. It has various static methods and fields defined that are directly accessible with the class name. And hence do not need an object for its invocation. The getProperty() method gets the system property indicated by the specified key, the line.separator variable in our case. It throws SecurityException if a security manager exists, and its checkPropertyAccess method doesn’t allow access to the specified system property. NullPointerException if the key is null. And IllegalArgumentException if the key is empty.

package new_line;

public class WaysToPrintNewLine {
  public static void main(String[] args) {
    String newline = System.getProperty("line.separator");
    System.out.println("I am in line1" + newline + "I am in line2");
  }
}

Below is the console output for the above code block.

I am in line1
I am in line2

The lineSeparator() method returns the system-dependent line separator string. It always returns the system-dependent string, the initial value of the system property that is the line.separator string. The method is available in Java 1.7 and later versions. The method is a static factory method of the System class.

package new_line;

public class WaysToPrintNewLine {
  public static void main(String[] args) {
    String newline1 = System.lineSeparator();
    System.out.println("I am in line1" + newline1 + "I am in line2");
  }
}

The output of the above code is similar to the System.getProperty code output.

The printf() method of the PrintStream class finds usage to write a formatted string to this output stream using the specified format string and arguments. This method is available since the Java 1.5 release. It throws java.util.IllegalFormatException if a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, and NullPointerException if the format is null. The %n character is a platform-independent character that we can use with printf() function as its format specifier.

package new_line;

public class WaysToPrintNewLine {
  public static void main(String[] args) {
    String newline1 = System.lineSeparator();
    System.out.printf("I am in line1%nI am in 2");
  }
}

The output of the above code is similar to the System.getProperty code output.

Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn

Related Article - Java Print