Declaring Text Blocks
Write multiline strings with triple quotes.
The Multiline Pain
Before Java 15, writing multiline text meant chaining strings with \n and +. It was hard to read and easy to get wrong. Text blocks solve this.
Triple Quotes
A text block opens with three double quotes """ followed by a newline, then your content, then a closing """. Everything between is the string.
public class Main {
public static void main(String[] args) {
String message = """
Hello
World
""";
System.out.println(message);
}
}All lessons in this course
- Declaring Text Blocks
- Indentation and Stripping
- Escapes in Text Blocks
- Use Cases: JSON and SQL