If you want to add a line break in your output string in your Java code, you can make use of the newline character java method from the System class called System.lineSeparator(),
Example 1: Java newline using System.lineSeparator()
public class JavaNewLineCharExample {
public static void main(String... args) {
String myString = "Hello" + System.lineSeparator() + "World!";
System.out.println(myString);
}
}
Output:
Hello
World!
Example 2: Using \r character
public class JavaNewLineCharExample {
public static void main(String... args) {
String myString = "Hello \r World!";
System.out.println(myString);
}
}
Output:
Hello
World!
Example 3: Using \n character
public class JavaNewLineCharExample {
public static void main(String... args) {
String myString = "Hello \r World!";
System.out.println(myString);
}
}
Output:
Hello
World!
Example 4: Using \r\n character
public class JavaNewLineCharExample {
public static void main(String... args) {
String myString = "Hello \r\n World!";
System.out.println(myString);
}
}
Output:
Hello
World!

Java newline example
Note: It is always better to use the example 1 approach of using System.lineSeparator() because it will use the new line character based on what platform you are using/executing the code, example 2, 3 and 4 are platform dependent.
Have Questions? Post them here!
More Posts related to Java,
- How to Get List of All Country Codes in Java Using Locale Class
- Unsupported major.minor version 52.0 in java
- Java - How to set custom thread name?
- Get the current timestamp in Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- java: unclosed string literal [Error]
- Convert Java Byte Array to String with code examples
- Error: Can not find the tag library descriptor for
- Java 8 - Convert List to Map Examples
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Fix java.net.ProtocolException: Invalid HTTP method
- Java: Convert Stream to List
- Java equals method - Tutorial
- List of Java JDBC Database Driver Jars, Classes and URLs Details
- Read YAML file Java Jackson Library
- How to display Java Date Time timezone GMT/UTC offset using SimpleDateFormat
- List of Java Keywords
- Enable JSON Pretty Print in Java Jackson
- How to Word-Warp Console logs in IntelliJ
- Convert Map to List in Java 8 using Stream API
- Create a Directory using Java Code
- Ways to Convert Integer or int to Long in Java
- [Program] How to read three different values using Scanner in Java
- Java JDBC Example with Oracle Database Driver Connection
More Posts:
- How to Access Terminal (Command Line) in Eclipse IDE - Eclipse
- How to check installed docker version command - Docker
- How to install XML Tools Plugin Notepad++ - NotepadPlusPlus
- Create simple struts2 project using maven commands - Java
- macOS Ventura XCode Command Line Tools Installation - MacOS
- Android Studio Change SDK Path - Android
- Fix: TypeError: can only concatenate str (not int) to str in Python - Python
- How to fix: You will need Google Chrome to install most apps, extensions and themes. - Chrome