Declaring Text Blocks
Write multiline strings with triple quotes.
Declaring Text Blocks is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Multiline Pain
Before Java 15, writing multiline text meant chaining strings with \n and +. It was hard to read and easy to get wrong. Text blocks solve this.
Triple Quotes
A text block opens with three double quotes """ followed by a newline, then your content, then a closing """. Everything between is the string.
public class Main {
public static void main(String[] args) {
String message = """
Hello
World
""";
System.out.println(message);
}
}The Opening Line Rule
The content must start on the line after the opening """. You cannot put text right after the opening delimiter. This newline is required syntax.
public class Main {
public static void main(String[] args) {
String poem = """
Roses are red
Violets are blue
""";
System.out.println(poem);
}
}Newlines Are Automatic
Each line break in a text block becomes a \n in the resulting string. You no longer write escape sequences for line endings.
public class Main {
public static void main(String[] args) {
String list = """
Apples
Bananas
Cherries
""";
System.out.print(list);
}
}No More Concatenation
Compare the old way ("a\n" + "b\n") with a text block. The text block reads exactly like the output, which makes mistakes obvious.
public class Main {
public static void main(String[] args) {
String html = """
<html>
<body>Hi</body>
</html>
""";
System.out.println(html);
}
}Quotes Without Escaping
Inside a text block, single double-quotes " need no escaping. This is perfect for content that contains quotes, like JSON.
public class Main {
public static void main(String[] args) {
String json = """
{"name": "Alice", "age": 30}
""";
System.out.println(json);
}
}A Text Block Is Still a String
The result is an ordinary String. You can call any String method on it, concatenate it, or pass it to other methods.
public class Main {
public static void main(String[] args) {
String text = """
one
two
""";
System.out.println("Length: " + text.length());
System.out.println("Upper: " + text.toUpperCase());
}
}Concatenating Text Blocks
You can join a text block with other strings using +, just like any string literal.
public class Main {
public static void main(String[] args) {
String name = "Bob";
String greeting = """
Dear customer,
""" + "Welcome " + name;
System.out.println(greeting);
}
}Empty Lines Are Preserved
Blank lines inside the block are kept as empty lines in the string. This lets you format spaced-out output naturally.
public class Main {
public static void main(String[] args) {
String spaced = """
Title
Body text
""";
System.out.print(spaced);
}
}Readable Code Snippets
Text blocks shine when embedding code-like content. Here a small block of pseudo-config stays perfectly readable.
public class Main {
public static void main(String[] args) {
String config = """
host = localhost
port = 8080
debug = true
""";
System.out.print(config);
}
}Counting Lines
Because newlines are real, you can split a text block on \n to process each line.
public class Main {
public static void main(String[] args) {
String data = """
red
green
blue
""";
String[] lines = data.split("\n");
System.out.println("Lines: " + lines.length);
}
}Quick Check
Where must the content of a text block begin?
Recap
You learned to declare text blocks:
- Open and close with
""" - Content must start on the next line
- Line breaks become
\nautomatically - Double quotes need no escaping
- The result is an ordinary
String
Frequently asked questions
Is the “Declaring Text Blocks” lesson free?
Yes — the full text of “Declaring Text Blocks” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Declaring Text Blocks”?
Write multiline strings with triple quotes. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Declaring Text Blocks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Text Blocks
- Indentation and Stripping
- Escapes in Text Blocks
- Use Cases: JSON and SQL