🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
Text blocks, introduced in Java 13 as a preview feature and made a standard feature in Java 15, provide a way to define multi-line string literals in a more readable and concise manner. Text blocks help improve code readability, reduce the need for escape sequences, and make it easier to work with large text content, such as HTML, JSON, or SQL queries.
Key Points:
- Multi-line Strings: Easily define strings that span multiple lines.
- Improved Readability: No need for concatenation or excessive escape sequences.
- Indentation Management: Handles common leading whitespace automatically.
Table of Contents
- Creating Text Blocks
- Advantages of Text Blocks
- Common Use Cases
- Examples
- Conclusion
1. Creating Text Blocks
Text blocks are created using triple-double quotes ("""). The opening triple quotes can be followed by a new line, and the closing triple quotes should be on their own line after the text content.
Syntax:
String textBlock = """
This is a text block.
It can span multiple lines.
No need for concatenation or escape sequences.
""";
Example:
public class TextBlockExample {
public static void main(String[] args) {
String textBlock = """
This is a text block.
It can span multiple lines.
No need for concatenation or escape sequences.
""";
System.out.println(textBlock);
}
}
Output:
This is a text block.
It can span multiple lines.
No need for concatenation or escape sequences.
2. Advantages of Text Blocks
Improved Readability
Text blocks make it easier to read and write multi-line strings without the need for concatenation or escape sequences.
Example:
Without Text Blocks:
String html = "<html>\n" +
" <body>\n" +
" <h1>Hello, World!</h1>\n" +
" </body>\n" +
"</html>";
With Text Blocks:
String html = """
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
""";
Reduced Need for Escape Sequences
Text blocks handle common escape sequences automatically, making the code cleaner.
Example:
Without Text Blocks:
String json = "{\n" +
" \"name\": \"John\",\n" +
" \"age\": 30\n" +
"}";
With Text Blocks:
String json = """
{
"name": "John",
"age": 30
}
""";
3. Common Use Cases
Use Case 1: Defining HTML Content
Text blocks are useful for defining HTML content in a readable manner.
Example:
public class HtmlExample {
public static void main(String[] args) {
String html = """
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
""";
System.out.println(html);
}
}
Use Case 2: Defining JSON Content
Text blocks are useful for defining JSON content without the need for escape sequences.
Example:
public class JsonExample {
public static void main(String[] args) {
String json = """
{
"name": "John",
"age": 30,
"city": "New York"
}
""";
System.out.println(json);
}
}
Use Case 3: Defining SQL Queries
Text blocks are useful for defining multi-line SQL queries in a readable manner.
Example:
public class SqlExample {
public static void main(String[] args) {
String sql = """
SELECT id, name, age
FROM users
WHERE age > 18
ORDER BY name;
""";
System.out.println(sql);
}
}
4. Examples
Example 1: Text Block with Indentation
Text blocks automatically handle common leading whitespace.
public class IndentationExample {
public static void main(String[] args) {
String indentedText = """
Line 1
Line 2
Line 3
""";
System.out.println(indentedText);
}
}
Example 2: Combining Text Blocks with Variables
You can combine text blocks with variables using concatenation or formatted strings.
public class VariableExample {
public static void main(String[] args) {
String name = "Alice";
String message = """
Hello, %s!
Welcome to the text block example.
""".formatted(name);
System.out.println(message);
}
}
Example 3: Handling Escape Sequences
Text blocks handle common escape sequences, such as newlines and tabs.
public class EscapeSequenceExample {
public static void main(String[] args) {
String textBlock = """
This is a text block.
It can handle escape sequences like \n for newlines and \t for tabs.
""";
System.out.println(textBlock);
}
}
Example 4: Multi-line Strings with Line Breaks
Text blocks can include line breaks directly within the text.
public class LineBreakExample {
public static void main(String[] args) {
String poem = """
Roses are red,
Violets are blue,
Text blocks are handy,
And so are you.
""";
System.out.println(poem);
}
}
5. Conclusion
Text blocks provide a powerful and convenient way to define multi-line string literals in Java. By leveraging text blocks, you can improve the readability and maintainability of your code, especially when working with large text content like HTML, JSON, and SQL queries. Understanding and using text blocks can significantly enhance your Java programming experience.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment