New line character in java

In this post, we will see about new line character in java and how to add new line character to a String in different operating systems.

Operating systems have different characters to denote the end of the line.

Linux and new mac:
In Linux, the end line is denoted by \n, also known as line feed.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Window:
In windows, end line is denoted by \r\n, also known as Carriage return and line feed (CRLF)

Old mac:
In older version of mac, end line is denoted by \r, also known as Carriage return.

Using \n or \r\n

You can simply add \n in linux and \r\n in window to denote end of the line.

Output:

Hello
world
Hello
world

You can use \n or \r\n but this method is not platform independent, so should not be used.

Using Platform independent line breaks (Recommended)

We can use System.lineSeparator() to separate line in java. It will work in all operating systems.

You can also use System.getProperty("line.separator") to put new line character in String.

Here is the quick snippet to demonstrate it.

Output:

Hello
world
Hello
world

As this method will work in all environment, we should use this method to add new line character in String in java.

That’s all about new line character in java.

Related Posts

  • Convert chartacter to ascii in java
    04 May

    Convert Character to ASCII Numeric Value in Java

    Learn about how to convert Character to ASCII Numeric Value in Java.

  • 30 September

    How to compare characters in Java

    Learn about how to compare characters in java using different methods.

  • 09 October

    Java program to print vowels in a String

    In this post, we will see how to print and count vowels in a string. Print Vowels in a String If any character in String satisfy below condition then it is vowel and we will add it to Hashset. character==’a’ || character==’A’ || character==’e’ || character==’E’ || character==’i’ || character==’I’ || character==’o’ || character==’O’ || […]

  • 06 October

    Java remove last character from string

    Learn about how to remove last character from String in java using different ways.

  • 06 October

    Convert char to lowercase java

    You can use Character class’s toLowerCase method to convert char to lowercase in java. Method signature [crayon-60924969455ae981433600/] Parameters ch is primitive character type. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to tech events 🗓️ Calendar.️ Return type return type is char. If char […]

  • 06 October

    Convert char to uppercase java

    You can use Charater class’s touppercase method to convert char to uppercase in java. Method signature [crayon-6092496945938633370605/] Parameters ch is primitive character type. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to tech events 🗓️ Calendar.️ Return type return type is char. If char […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.