Sitemap
Santiago

Santiago

Java Text Blocks

3 min readDec 29, 2024

--

Java introduced Text Blocks (also called String Blocks) as a new feature in Java 15 (after being previewed in Java 13 and 14). This feature provides a more readable and developer-friendly way to write multi-line strings.

Get Santiago’s stories in your inbox

Join Medium for free to get updates from this writer.

A Text Block is a multi-line string literal enclosed in triple double quotes ("""). It simplifies the creation of strings that span multiple lines, such as HTML, JSON, SQL queries, or other structured text.

Key Syntax

String textBlock = """
This is a text block.
It spans multiple lines.
""";

Text Blocks eliminate the need for explicit newline characters (\n) or concatenation (+).
They avoid excessive escaping of special characters.
Makes embedding code (e.g., SQL, JSON, XML) in Java much cleaner.

Example Comparison: Traditional Strings vs Text Blocks

Using a traditional multi-line string

String traditionalString = "This is a traditional string\n" +
"that spans multiple lines\n" +
"and requires concatenation.";

Using a text block

String textBlock = """
This is a text block
that spans multiple lines
without requiring concatenation.
""";

Differences Between Strings and Text Blocks

  • String: A regular Java String uses single double quotes (") and may require explicit formatting for multi-line text.
  • Text Block: A Java Text Block uses triple double quotes (""") and preserves the text's structure, including line breaks and indentation.

Comparing Strings

public class TextBlock {
public static void main(String[] args) {
final String traditionalString = "Hello Java 17";
final String textBlock1 = """
Hello Java 17""";
final String textBlock2 = """
Hello Java 17""";

// Compare content
System.out.println("Using equals(): " + traditionalString.equals(textBlock1));
System.out.println("Using equals(): " + textBlock1.equals(textBlock2));
// Compare references
System.out.println("Using ==: " + (traditionalString == textBlock1));
System.out.println("Using intern(): " + (textBlock1.intern() == textBlock2.intern()));
}
}

Output

Using equals(): true
Using equals(): true
Using ==: true
Using intern(): true

final String traditionalString = "Hello Java 17";

This is a String literal. All string literals are stored in the String Pool to optimize memory usage.

final String textBlock1 = """ Hello Java 17""";

This is a Text Block. Because its content is identical to traditionalString, the compiler optimizes the string and stores it in the String Pool, making traditionalString and textBlock1 point to the same memory location.

final String textBlock2 = """ Hello Java 17""";

Similar to textBlock1, the content of textBlock2 is "Hello Java 17". The compiler identifies this and stores the same reference in the String Pool.

traditionalString.equals(textBlock1)

The .equals() method compares the content of the two strings.
Since both traditionalString and textBlock1 have the same content ("Hello Java 17"), this returns true.

textBlock1.equals(textBlock2)

Again, .equals() compares the content.
The content of textBlock1 and textBlock2 is identical, so this returns true.

traditionalString == textBlock1

The == operator compares the references (memory locations).
Because the compiler stores both traditionalString and textBlock1 in the same String Pool reference due to optimization, the references are the same. This returns true.

textBlock1.intern() == textBlock2.intern()

The intern() method ensures that strings are stored in the String Pool.
Since textBlock1 and textBlock2 are already in the pool and have identical content, their references are the same. This returns true.

Use Cases for Text Blocks

Embedding Structured Data: Text Blocks simplify embedding XML, JSON, or HTML.

String json = """
{
"name": "Santiago",
"age": 30
}
""";

SQL Queries:

String sql = """
SELECT *
FROM employees
WHERE department = 'HR';
""";

Readability: Improves code readability when working with large, multi-line strings.

Traditional strings can become hard to read and maintain when dealing with multi-line content, requiring concatenation and escaping. Text Blocks address these issues by:

  • Removing the need for manual formatting.
  • Keeping the string’s natural structure, improving readability and maintainability.

--

--