Ad space (banner)
โ˜•Java Lessons
Lesson 46 / 68

Multi-Line Strings (Text Blocks)

This lesson covers writing a multi-line string with Java's text blocks, so you can build a readable message. It's for anyone searching "Java text block usage."

Wrapping text in three double quotes lets you write a multi-line string (a text block) that includes line breaks directly. It's a relatively recent Java feature, handy for writing something like an email body or a long explanation readably.

The example includes a %s placeholder inside a text block, filling it in with a variable using .formatted(name). It builds a 3-line message โ€” a greeting, a thank-you, and a closing โ€” with the line breaks included directly.

A common early mistake is how text blocks handle indentation. The leading whitespace at the start gets automatically stripped, which behaves differently from a regular string and takes some getting used to.

In real projects, this is used a lot for building readable text like SQL statements or JSON โ€” anywhere you're constructing a long piece of text.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)