Java Text Blocks
Multi-line string literals with improved readability (Java 15+)
java (15+)
2025-11-03
text-blocks
strings
multiline
java15
Description
Text blocks are multi-line string literals that make it easier to work with strings that span multiple lines. They were introduced as a preview feature in Java 13 and became standard in Java 15.
Key Features
- Multi-line strings: Write strings across multiple lines naturally
- Automatic formatting: Preserves indentation and whitespace
- Escape sequences: Support for escape sequences
- Interpolation: Can use expressions (future feature)
Syntax
- Start with
"""(three double quotes) - Content on the next line
- End with
""" - Indentation is relative to the closing delimiter
Use Cases
- SQL queries
- JSON/XML strings
- HTML templates
- Multi-line messages
- Code generation
- Configuration strings
Code
public class TextBlockUtils { // Basic text block public static String getMultiLineText() { return """ This is a multi-line text block """; } // SQL query as text block public static String getSelectQuery() { return """ SELECT id, name, email FROM users WHERE status = 'ACTIVE' ORDER BY name """; } // JSON as text block public static String getJsonExample() { return """ { "name": "John Doe", "age": 30, "city": "New York" } """; } // HTML template as text block public static String getHtmlTemplate(String title, String content) { return """ <html> <head> <title>%s</title> </head> <body> <h1>%s</h1> <p>%s</p> </body> </html> """.formatted(title, title, content); } // Text block with escape sequences public static String getTextWithEscapes() { return """ Line 1 Line 2 with "quotes" Line 3 with \n newline """; } // Text block preserving formatting public static String getFormattedCode() { return """ public class Example { public static void main(String[] args) { System.out.println("Hello"); } } """; }}
// Basic text blockString text = """ This is a multi-line string """;// SQL queryString query = """ SELECT u.id, u.name, u.email FROM users u WHERE u.status = 'ACTIVE' AND u.created_at > '2024-01-01' ORDER BY u.name """;// JSON stringString json = """ { "name": "Alice", "age": 25, "hobbies": ["reading", "coding"] } """;// HTML templateString html = """ <div class="container"> <h1>Welcome</h1> <p>This is a paragraph</p> </div> """;// XMLString xml = """ <person> <name>John</name> <age>30</age> </person> """;// Using with String methodsString formatted = """ Hello %s, Welcome to %s! """.formatted("Alice", "Java");// Text block with escape sequencesString withEscapes = """ Normal text Text with \"quotes\" Text with \\backslash """;// Code templateString codeTemplate = """ public class %s { private String %s; public %s(String %s) { this.%s = %s; } } """.formatted("Person", "name", "Person", "name", "name", "name");// Multi-line messageString message = """ Error occurred: - File not found - Permission denied Please check the file path. """;
Comments
No comments yet. Be the first to comment!
Please login to leave a comment.