Skip to main content

How to Write a Text Block in Java

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

A text block is a Java string written between """ and """, over as many lines as you like, with double quotes needing no backslashes. It's the easiest way to put JSON in Java code, and where you put the closing """ decides whether the text ends with a line break.

Syntax​

Syntax
String <text> = """
<line>
<line>""";

String <text> = """
<line>
<line>
""";
PartWhat it does
Opening """Must be followed by a line break; the text starts on the next line.
Closing """ on the last text lineThe text ends without a line break.
Closing """ on its own lineThe text ends with a line break.
IndentationThe spaces shared by every line, the closing line included, are removed.

The Java Language Specification, section 3.10.6: "A text block consists of zero or more characters enclosed by opening and closing delimiters." And: "The opening delimiter is a sequence that starts with three double quote characters ("""), continues with zero or more space, tab, and form feed characters, and concludes with a line terminator."

The Example​

The example writes a short piece of JSON as a text block that closes on its own line of text, the same JSON spread over several lines with the closing """ on a line of its own, and then checks whether the second one ends with a line break.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which declares json as a text block that closes on the same line as the JSON, and pretty as a four-line text block whose closing delimiter has its own line, then prints both and whether pretty ends with a newline.

Main.java
public class Main {
public static void main(String[] args) {
String json = """
{"name":"Ada","role":"admin"}""";
String pretty = """
{
"name": "Ada",
"role": "admin"
}
""";

System.out.println(json);
System.out.print(pretty);
System.out.println(pretty.endsWith("\n"));
}
}

Step 2: Run It​

Terminal
java Main.java
Output
{"name":"Ada","role":"admin"}
{
"name": "Ada",
"role": "admin"
}
true

A zsh terminal where java Main.java prints the one-line JSON, the same JSON over four lines starting at the left edge, and true.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • json printed its double quotes as they are. Its closing """ sits on the line of text, so there's no line break at the end.
  • pretty lost the indentation it shared with its closing line, so the JSON starts at the left edge. Its closing """ has a line of its own, so the text ends with a line break, which is why the code prints it with print instead of println.
  • pretty.endsWith("\n") confirmed the line break: true.

Common Mistakes​

MistakeWhat happensDo this instead
Putting text right after the opening """error: illegal text block open delimiter sequence, missing line terminatorStart the text on the next line
Putting the closing """ on its own line when you don't want a line breakThe string ends with \n, which breaks exact comparisonsClose on the last line of text
Expecting \n inside a text block to stay as two charactersEscape sequences still work: a text block holding a\nb gave two linesWrite \\n for a literal backslash and n

Troubleshooting​

SymptomLikely causeFix
An extra blank line when printingThe text ends with a line break and println adds anotherUse print, or close the block on the last text line
Unexpected spaces at the start of each lineThe closing """ sits further left than the textMove it to the same indentation as the text

FAQ​

Which Java version do I need?​

Java 15 or later. Oracle's Programmer's Guide to Text Blocks: "JEP 378 adds the language feature text blocks to Java SE 15 and later."

Can I insert values into a text block?​

Not with special syntax, but you can call formatted on one: a text block holding Hello, %s!, followed by .formatted("Ada"), gave Hello, Ada!.

Conclusion​

Put text between a """ that ends its line and a closing """, and write it exactly as it should appear, quotes and all. Close on the last line of text for no trailing line break, or on a line of its own to get one.

Additional Resources​