Text Blocks in Java 15 feel like a breath of fresh air if you write code that touches JSON, HTML, or SQL on a daily basis.
We have early access builds of Java 15 and the feature that keeps pulling me back is Text Blocks. The official tag is JEP 378 and it moves multi line strings from a preview into a real thing. No flags. No awkward syntax games. Just triple quotes and clean text. If you lived through the jungle of escaped quotes and plus signs every time you wrote a SQL query or a JSON sample for a test, you know exactly why this matters for readability, for diff quality, and for your sanity.
At its core a text block is a string literal that spans lines using three double quotes. You write your content almost as you would in a file and the compiler helps with incidental indentation so your code stays tidy without smuggling spaces into the string. Newlines and escapes behave in a familiar way. You still have \n and \t, and you also get a neat backslash at the end of a line to skip the newline when you want a long logical line in source without a real line break in the value. That tiny trick already cleaned up a few ugly builders in my codebase.
Here is a quick taste with JSON, a classic pain point when embedded in Java:
String json = """
{
"name": "Ada",
"skills": [
"math",
"logic",
"writing"
]
}
""";No backslash party. No quote salad. Your eyes can scan it as if it were a .json file. The compiler trims leading whitespace that is common to all lines inside the block so the nested braces look right in your editor and the resulting string is exactly what you expect. If you need to include three quotes in a row inside the content, escape one of them and keep going:
String tricky = """
Say \"\"\"hello\"\"\" to triple quotes inside a text block.
""";HTML is another winner. Templates look like templates again. I have been wiring small snippets for emails and admin pages without the usual mess of concatenation. Bonus points when used with String::formatted which landed recently and reads better than String.format chained calls.
String li = """
%s
""".formatted(url, label);SQL benefits the most in my day to day work. Long selects that used to be broken into chunks with plus signs are now pasted as is. This cuts bugs produced by missing spaces during concatenation and makes code review pleasant. You can even keep the query aligned with your Java indentation and the result still comes out clean.
String query = """
select u.id, u.email, r.name as role_name
from users u
join roles r on r.id = u.role_id
where u.status = 'ACTIVE'
order by u.created_at desc
""";There are a couple of small tricks worth knowing. The closing triple quotes go on their own line in most cases, which makes the start and end clear. If you want to avoid the trailing newline at the end of the block, use a backslash on the previous line. You can also reach for helper methods that play nice with text blocks like stripIndent and translateEscapes. They help when you paste content from somewhere else and want to normalize it without manual edits.
String path = """
C:\\Program Files\\Java\\jdk
""".translateEscapes();
String csv = """
name,age,city \
country
"""; // the backslash removes the newline before countryTooling is catching up. IntelliJ and friends already highlight text blocks nicely in the current builds and the early access JDK behaves the way the spec describes. Tests that embed sample payloads got way easier to read. I noticed my diffs are smaller because I am not shuffling quotes and plus signs around. That alone will save time for any team doing code review over remote calls right now. We all read more code than we write. Making it comfortable counts.
If you work with templating libraries or with JSON binding, text blocks fit right in. You can still use parameterized values with formatted or with your favorite templating tool. You can also store snippets next to your code for integration tests, which beats juggling resource files for tiny cases. The key advantage is obvious in the first commit that uses them. Readability. Fewer escapes. Cleaner diffs. Easier review. I did not expect such a small feature on paper to feel this nice in practice.
If you like to keep your codebase friendly for older runtimes, remember this is a Java 15 feature. For libraries that must support older versions, you can still use text blocks in tests while keeping main sources on older syntax.
Java 15 is shaping up nicely and text blocks are the quiet star for many teams that move strings around all day.
Give them a try in an early access build and paste a real query or a real JSON into your code.