0Pricing
Java Academy · Lesson

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

  1. Declaring Text Blocks
  2. Indentation and Stripping
  3. Escapes in Text Blocks
  4. Use Cases: JSON and SQL
← Back to Java Academy