User Brian
Brian
Level 41

How to get a new line character in Java?

Published in the Java Developer group

What are the newline characters in Java?

The reserved characters “\n” or “\r\n” in Java are called the newline characters.
In Java, there are allocated keywords for performing specific functions. For example, using the keyword “int” will make the datatype of a variable an integer. Likewise, using “\n” or “\r\n” while printing any String in Java, a “backslash n” won’t be printed. Instead, a new line break will be printed on the console.

What does “\n” print in Java?

Let’s look at an example of printing “\n” on the console.

Example


public class NewLineChar {
	public static void main(String[] args) {

		System.out.println("Hi, I am Sponge Bob.\nI just signed up at Code Gym.\nI love the Java learning experience here so far!");
	}
}

Output

Hi, I am Sponge Bob. I just signed up at Code Gym. I love the Java learning experience here so far!

How to add a newline character to a String?

There are multiple ways of adding the newline character to a string. You can use either “\n” or “\r\n” to add a line break in a continuous paragraph. Let’s look at some ways of doing so.

Example


public class NewLineChar {
	public static void main(String[] args) {

		// Method 1: newline after every sentence
		String sentence1 = "What a beautiful day to be alive!";
		String sentence2 = "Let's be thankful for all the blessings we've been showered with.";
		String sentence3 = "It only makes sense to be helpful, cooperative and generous to those who are less fortunate.";

		// without newline after each sentence
		System.out.println("-------Printing without newline character--------");
		System.out.println(sentence1 + sentence2 + sentence3);

		// newline character after each sentence
		System.out.println("\n-------Printing with newline character-----------");
		System.out.println(sentence1 + "\n" + sentence2 + "\n" + sentence3);

		// Method 2: new line after each paragraph
		String paragraph1 = "Java is the most dynamic and easy to plug and play language. It has been transforming lives for more than 2 decades now. It still continues to grow to date.";
		String paragraph2 = "If you're a beginner who wants to be a professional in Java, you need to focus on targeted learning with consistent practice. If you're looking for a responsive community to grow with, then Code Gym is the way to go!";

		// without newline after each paragraph
		System.out.println("\n-------Printing without newline character--------");
		System.out.println(paragraph1 + paragraph2);

		// new line character after each paragraph
		System.out.println("\n-------Printing with newline character-----------");
		System.out.println(paragraph1 + "\r\n" + paragraph2);
	}
}

Output

-------Printing without newline character-------- What a beautiful day to be alive!Let's be thankful for all the blessings we've been showered with.It only makes sense to be helpful, cooperative and generous to those who are less fortunate. -------Printing with newline character----------- What a beautiful day to be alive! Let's be thankful for all the blessings we've been showered with. It only makes sense to be helpful, cooperative and generous to those who are less fortunate. -------Printing without newline character-------- Java is the most dynamic and easy to plug and play language. It has been transforming lives for more than 2 decades now. It still continues to grow to date.If you're a beginner who wants to be a professional in Java, you need to focus on targeted learning with consistent practice. If you're looking for a responsive community to grow with, then Code Gym is the way to go! -------Printing with newline character----------- Java is the most dynamic and easy to plug and play language. It has been transforming lives for more than 2 decades now. It still continues to grow to date. If you're a beginner who wants to be a professional in Java, you need to focus on targeted learning with consistent practice. If you're looking for a responsive community to grow with, then Code Gym is the way to go!

Conclusion

By now you should be comfortable with the use of the newline characters in Java. For a deeper command of the subject, we advise you to continue practising with the newline characters in Strings in Java. Good luck and happy learning!How to get a new line character in Java? - 1
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION