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!
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!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- Program 3: Print the name of user as input - 1000+ Python Programs - Python-Programs
- 10 ways to Convert String to a Number in JavaScript - JavaScript
- How to Hum a Song to Google to find it out! [Android and iPhone] - Google
- How to empty trash in Android Device - Android
- How to Manage Profile Picture on Microsoft Teams - Teams
- How to open TextEdit from Mac Terminal - MacOS
- How to Copy full Absolute Path of a File on Mac - MacOS
- Python: pandas merge DataFrames on Common Column - Python