System.lineSeparator() method in Java With Examples

The lineSeparator() is a built-in method in Java which returns the system-dependent line separator string. It always returns the same value – the initial value of the system property line.separator.

Syntax:

public static String lineSeparator()

Parameters: This method does not take any parameters.



Return Values: In any UNIX systems, it will return “\n” or a positive integer; and on Windows systems it returns “\r\n” or a positive integer.

Exception : It throws NullPointerException , if the string is null

Below programs illustrate the System.lineSeparator() method:

Program 1: To illustrate the working of static String lineSeparator() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of static String lineSeparator() method
import java.io.IOException;
import java.lang.*;
import java.nio.channels.Channel;
public class LineSeparatorExample {
  
    public static void main(String[] args)
    {
        String s = System.lineSeparator();
  
        for (char c : s.toCharArray()) {
            System.out.println((int)c);
        }
    }
}

chevron_right


Output:

10

Note: Here it returns 10. So here 10 is the line separator.

Program 2: To illustrate the working of static String lineSeparator() method for an integral value.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of static String lineSeparator() method
import java.io.IOException;
import java.lang.*;
import java.nio.channels.Channel;
class SystemDemo {
    public static void main(String args[])
        throws NullPointerException,
               IOException
    {
        Integer x = 636;
        System.out.println(System.lineSeparator());
    }
}

chevron_right


Output:

\r\n

Note: Here it returns “\r\n” since its a Microsoft Windows systems.

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#lineSeparator()



My Personal Notes arrow_drop_up

lets make it a lil simple

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.