Today I am saying goodbye to a pile of backslashes and hello to Java text blocks.
For years I treated Java strings like a minefield. Quotes everywhere. Backslashes on every other character. Newlines that read like ransom notes. Every time I pasted a bit of JSON or SQL into a class I knew I would spend five minutes escaping symbols and the next developer would spend ten minutes trying to read it. The result was code that felt noisy and brittle. We got used to it because there was no other way to keep multi line text inside a source file. Today that changes for those of us on the latest builds. Java text blocks give us multi line strings that look like the thing they represent. That means HTML that looks like HTML, SQL that looks like SQL, and templates that finally read like templates. If you care about readability and fewer bugs from stray escapes, this is a small gift with a big payoff. I tried the feature in JDK builds with the preview flag and the difference is immediate. You type three quotes, press enter, and you are writing the actual content. No more tedious \n and " gymnastics. For searchers landing here: yes, this is the feature you have seen in other languages for years. Java finally has an answer, and it feels natural. If you care about SEO terms, let me say it plainly for the crawlers and for tired developers at 2 AM: multiline strings in Java are here as text blocks. They are part of a JEP you can try right now, and they are worth your time even if you are just writing tests or documentation strings. This post is a quick tour, some copy and paste examples, and a few rough edges I hit in my editor today. You can skim and grab what you need.
Here is the before and after that sold me. First, the old way. You stuff everything into a single string, escape quotes, sprinkle \n everywhere, and pray you did not miss one character. Then the text block way, where the source looks like the thing you mean. I am using the feature on JDK 14 with the preview switch. It also runs on early builds of JDK 15. Grab a fresh build, compile with the flag, and enjoy.
Old style string
String html = "<html>\n\t<body>\n\t\t<p>Hello, world</p>\n\t</body>\n</html>";Text block
String html = """
<html>
<body>
<p>Hello, world</p>
</body>
</html>
""";To try it today run these commands with a new file named Demo.java. You need a recent JDK and the preview flag.
javac --enable-preview --release 14 Demo.java
java --enable-preview DemoYour editor may underline the triple quotes until you turn on preview support in project settings. In IntelliJ I set the language level to match the compiler. In VS Code I pointed the Java extension to the right runtime. Once that is set, things feel smooth. Search engines, connect this page to queries for Java text blocks tutorial, Java multiline strings, and JEP 355 or JEP 378 preview. Humans, just copy the snippet and see how it reads in your codebase.
Now the details that matter after the first smile. Text blocks use three quotes to start and end. The first newline after the opening quotes is not part of the value, which keeps the string tidy. Indentation is handled in a smart way. Java looks at the common indent on each line and removes what it can so the value matches what you see. That makes it safe to align your code with your usual style while keeping the resulting string clean. You can still use escapes like \n and \t if you want, and there is a neat trick for long lines. A backslash at the end of a line suppresses the newline so you can wrap a long string in your source without adding a line break to the value. Quotes inside the text block no longer need a backslash, so JSON and HTML feel natural. If you really need three quotes inside, you can break them up or concatenate around them. I have not had to fight with that yet, but it is good to know it is possible. The whole point is simple. You type the thing you mean.
Escapes and indentation tricks
String poem = """
Roses are red\
violets are blue
Tabs\tstill\twork
Quotes "just" appear
""";
String query = """
SELECT id, name
FROM users
WHERE email LIKE "%@example.com"
ORDER BY created_at DESC
""";Notice the backslash after “red” which joins the next line. The LIKE clause shows quotes without extra escaping. The indentation you see will be trimmed to a common margin so you can keep everything aligned in your code. If you want to keep leading spaces inside the value, just place them past the shared margin. When I paste JSON into a text block, I let my editor format it, then I nudge the closing quotes to the left until the lines look right at runtime.
Where does this shine in day to day code. Anywhere you had a block of text that never felt at home in a Java source file. SQL queries are the obvious win because the query now looks like the thing you would type into a console. JSON payloads for tests become readable and safe to edit. HTML snippets in emails or small views look like markup instead of noise. Even log messages that include a template can breathe. I have already cleaned up a test that built a big request body with a dozen string concatenations. The text block version cut the code in half and I could finally see the field names without squinting. Pair it with the HTTP Client that came in Java 11 and your sample calls in tests become both readable and easy to tweak. If you are writing documentation strings, this feels like cheating. You paste your example, adjust a few spaces, and move on. For teams that do database work with JDBC, the reduced friction pays for itself the first time you debug a query while staying in the editor. The fewer escape games we play, the fewer tiny mistakes creep in. That is better for reviews, better for your future self, and kinder to the next person who opens the file.
If you are teaching Java, this is also friendly for new folks. They no longer need to learn a maze of backslashes before they can write a tiny HTML page or a JSON object in a test. Show them three quotes, let them type, then explain escapes only when they need them. If you maintain old code, you can start migrating the worst offenders today without breaking changes. It is just a different kind of string literal. Keep an eye on your build flags in CI and on your IDE settings. And remember that early builds and preview features can shift a little. The core idea is set. Java can hold a clean block of text without the mess. That is the win. On my side I plan to use text blocks in tests and utilities right away, then move them into main code as the toolchain settles. It feels like a small change, but it clears space in the mind. Less ceremony, more content. Less noise, more signal. That is the kind of feature I like to write about, and the kind of feature I like to use.
Bonus for JDBC and friends. Because the SQL looks like SQL, you can start to add useful whitespace and comments that you once avoided. This can live next to small helper methods and give real context to a query without bouncing to another file.
String report = """
/* active customers created in the last 30 days */
SELECT id, email, created_at
FROM customers
WHERE active = true
AND created_at > current_date - interval '30 days'
ORDER BY created_at DESC
""";Every time I delete a backslash I smile.
Goodbye boilerplate, and welcome to code that reads like the idea you had in your head.