New Line In Java String

New Line In Java String | As we all know Java has many features. So in this blog, we will discuss one of that named new lines. To be more clear in this blog, we study the new line character and also see how to add a new line in a string through the Java programs.

We usually use ‘\n’ slash ‘n’ as a new line character. When we add it to the print statement then it prints the string or statement followed in a new line. There are many ways to add a new line so let us see them one by one.

New Line in Java String Using ‘\n’

We can use platform-dependent new line character i.e. ‘\n’ to add a new line to the string. When we print this string, the given string will be displayed in multiple lines.

public class Main {
   public static void main(String[] args) {
      System.out.println("Java" + '\n' + "Program");
   }
}

Output:-

Java
Program

New Line in Java String using System.lineSeperator()

We can use the System.lineSeperator() method to add a new line to the string. The syntax of lineSeperator() method of System class is:- public static String lineSeparator()

The lineSeparator() method returns the system-dependent line separator string. On UNIX systems, it returns “\n”; on Microsoft Windows systems it returns “\r\n”.

public class Main{
   public static void main(String[] args) {
      String newline = System.lineSeparator();
      System.out.println("Hello" + newline + "World");
   }
}

Output:-

Hello
World

New Line in String Java using System.getProperty()

We can use System.getProperty() method to add a new line to the string. The method syntax of the getProperty() method of the System class is:- public static String getProperty(String key). To get the newline we have to pass key = “line.separator“.

public class Main {
   public static void main(String[] args) {
      String newline = System.getProperty("line.separator");
      System.out.println("Hi" + newline + "Welcome");
   }
}

Output:-

Hi
Welcome

Java Print New Line in String

Similar to the new line character ‘\n’ we can also use the ‘%n’. But to use ‘%n’ we have to use the printf() method. The below Java program demonstrate it to print the new line in a string by using ‘%n’.

public class Main {
   public static void main(String[] args) {
      System.out.printf("Java%nProgram");
      System.out.println();

      String newline = "%n";
      System.out.printf("Hi" + newline + "Welcome");

      System.out.println();
      System.out.printf("%s%n%s", "Hello", "World");

      System.out.println();
      System.out.println("Hi" + newline + "Welcome");
   }
}

Output:-

Java
Program
Hi
Welcome
Hello
World
Hi%nWelcome

Add A New Line After String in Java

If want to display strings in a new line then we can simply use println() method. Calling System.out.println() method adds a new line character to the end of the line.

public class Main {
   public static void main(String[] args) {
      System.out.println("Java");
      System.out.println("Programming");
      System.out.println("Language");
   }
}

Output:-

Java
Programming
Language

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *