Skip to content
Related Articles

Related Articles

Improve Article

Java Program to Print a New Line in String

  • Last Updated : 06 Jun, 2021

Java is the most powerful programming language, by which we can perform many tasks and Java is an industry preferable language. So it is filled with a huge amount of features. Here we are going to discuss one of the best features of Java, that is how to print a new line in a string using Java.

Methods: 

There are many ways to print new line in string been illustrated as below:

  1. Using System.lineSeparator() method
  2. Using platform-dependent newline character
  3. Using System.getProperty() method
  4.  Using %n newline character
  5. Using System.out.println() method 

Let us discuss them individually in detail.

Method 1: Using System.lineSeparator() method



Example

Java




// Java program to print a new line in string
// Using System.lineSeparator() method
  
// Main class
class GFG {
    // Main Driver Code
    public static void main(String[] args)
    {
        // Calling the System.lineSeparator() function to
        // print newline in between some specified strings
        String newline = System.lineSeparator();
  
        // Printing new line
        System.out.println("GFG" + newline + "gfg");
    }
}
Output
GFG
gfg

Method 2: Using platform-dependent newline character. 

Note: Here the new line character is “\n” for Unix and “\r\n” for Windows OS.

Example 

Java




// Java program to print a new line in string
// Using platform-dependent Newline Character
  
// Main class
class GFG {
    
    // Main Driver Code
    public static void main(String[] args)
    {
        // Using new line Character '\n' to print
        // new line in between strings
        System.out.println("GFG" + '\n' + "gfg");
    }
}
Output
GFG
gfg

Method 3: Using System.getProperty() method. Here this function uses the value of the system property “line.separator”, which returns the system-dependent line separator string. 

Example 



Java




// Java program to print a new line in string
  
class GFG
{
    // Main Driver Code
    public static void main(String[] args)
    {
          
        // Calling System.getProperty() function Over The 
        // parameter for the value of the system property "line.separator",
        // which returns the system-dependent line separator string. 
        String newline = System.getProperty("line.separator");
          
        // Printing new line between two strings
        System.out.println("GFG" + newline + "gfg");
    }
}

Output: 

GFG
gfg

Method 4: Using %n newline character

Note: Here this newline character is used along with the printf() function.

Example 

Java




// Java program to print a new line in string
// Using %n Newline Character
  
// Importing input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Main Driver Code
    public static void main(String[] args)
    {
        // Printing new line using new line
        // Character "%n" with the printf() method
        System.out.printf("GFG%ngfg");
    }
}
Output
GFG
gfg

Method-5: Using System.out.println() method.

Example 

Java




// Java program to print a new line in string
// Using System.out.println() method
  
// Main class
class GFG {
    
    // Main driver method
    public static void main(String[] args)
    {
        // Printing new line using
        // System.out.println() function
        // over custom string inputs
        System.out.println("GFG");
        System.out.println("gfg");
    }
}
Output
GFG
gfg

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :