Skip to content

Simplified Learning Blog

Learning made easy

  • Home
  • Java
    • Core Java Tutorial
    • Java 8
    • What is Rest API in java
    • Spring Framework
    • Type Casting in Java | 2 types Implicit and explicit casting
    • JUnit 5 Tutorial
      • Assertall in JUnit 5
      • Assertions in JUnit 5
  • Java Interview Questions
    • Top 50 Core Java Interview Questions & Answers (2026 Edition)
    • Top 20 Spring Boot Interview Questions for Freshers (2026 Edition): The Ultimate Cheat Sheet
    • Top 40+ Multithreading interview questions
    • Top 10 AWS Lambda interview questions
  • Java Thread Tutorials
    • How to create thread in Java
    • Multithreading in java
    • Daemon Thread in Java | How to create daemon thread in java
    • Java Virtual Threads (Project Loom) in Real Enterprise Applications
    • WebFlux vs. Virtual Threads in 2026: The Senior Architect’s Decision Matrix
  • AWS
    • What is AWS (Amazon Web Services)
    • AWS IAM (Identity and Access Management)
    • AWS SNS | What is SNS
    • What is SQS | AWS SQS (Simple Queue Service)
    • What is AWS Lambda
  • Software Architecture
    • Software Architecture Performance
    • Performance Principles of Software Architecture
    • Practical System Design Examples using Spring Boot, Queues, and Caches (2026 Guide)
    • System Performance Objective
  • Spring Boot Tutorial
    • Spring Boot Rest API Example complete guide
    • Spring MVC vs. Spring Boot in 2026: The Senior Architect’s Definitive Guide
    • Spring Boot Application.properties vs. YAML: The 2026 Architect’s Verdict
  • Core Java Deep Dives
    • Java int to String Conversion: Performance Benchmarks & Memory Pitfalls
    • String to Integer Conversion in Java | Java convert string to int
    • Converting PDF to JSON in Java Top 3 ways to code:
    • Calculate date of birth from age in jquery
    • How to convert excel to PDF using java
    • jcalendar in java swing example
    • Series program in java
  • Tools
    • JSON Formatter & Debugging Guide for Spring Boot Developers
    • Free Character Counter Tool: The Ultimate Guide to Counting Characters, Words, and Text Statistics
  • Tech Blogs
    • Java 21 New Features
    • Is Java Dead? Is java dead, 2023 ?
    • New Features in Java 17
  • Toggle search form

Java Text Blocks

Posted on March 31, 2023March 31, 2023 By Govind No Comments on Java Text Blocks

Text blocks are introduced in java 13 and 14 as a preview feature and with java 15 it is available as standard feature.

Table of Contents

Toggle
  • What is text block in java ?
    • Introduction
    • Problem statement
    • Java Text Blocks
    • Escape Sequences in Text Blocks
      • Backslash at the End of the Line
      • Escape Sequence: \s
  • Conclusion

What is text block in java ?

Introduction

Java Text Blocks are available from java 15 as a standard feature, which is a string that starts with three double quotes (“””).

Problem statement

Previously, before the release of text blocks, we use multiline string literals like this

package com.slb.java;

public class StringTextBlockDemo {

    public static void main(String[] args) {

        String str ="This is simplified learning blog, where we provide tutorials for all java releted tech stack" +
                "where in we aim at explaing all these features in simplest form possible and" +
                "this is just an example of multiline " +
                "strings";
        System.out.println(str);

    }
}
Output 
This is simplified learning blog, where we provide tutorials for all java releted tech stackwhere in we aim at explaing all these features in simplest form possible andthis is just an example of multiline strings

Process finished with exit code 0

The issue here is that the code is messy, and It’s difficult to read as well as maintain. Consider a use case wherein you are returning HTML code like this :

package com.slb.java;

public class StringTextBlockDemo {

    public static void main(String[] args) {

        String str ="This is simplified learning blog, where we provide tutorials for all java releted tech stack" +
                "where in we aim at explaing all these features in simplest form possible and" +
                "this is just an example of multiline " +
                "strings";
        System.out.println(str);

        String strHtmlContent = "<!DOCTYPE html>\n" +
                "<html>\n" +
                "    <head>\n" +
                "        <!-- head definitions go here -->\n" +
                "    </head>\n" +
                "    <body>\n" +
                "        <!-- the content goes here -->\n" +
                "    </body>\n" +
                "</html>\n";
    }
}
OP
<!DOCTYPE html>
<html>
    <head>
        <!-- head definitions go here -->
    </head>
    <body>
        <!-- the content goes here -->
    </body>
</html>

Now if you look at the first example it was quite okay as compared to this HTML block of code. Another alternate is to use string builder, but that is also not that easy to read and understand.

Java Text Blocks

So to solve this above issue, we have java text blocks to rescue. We will place this HTML code inside three double-quoted string as:

    package com.slb.java;
    
    public class StringTextBlockDemo {
    
        public static void main(String[] args) {
            String strHtmlContent = """
                    <!DOCTYPE html>
                    <html>
                        <head>
                            <!-- head definitions go here -->
                        </head>
                        <body>
                            <!-- the content goes here -->
                        </body>
                    </html>
                     """;
            System.out.println(strHtmlContent);
        }
    
    }

Now, with the above code, it will be easier to read and understand. Note that, java text blocks returns string only.

We can also indent our code.

Escape Sequences in Text Blocks

String blocks have the advantage of escaping common escape sequences like \” and \n as they have no effect on the string and not required.

Backslash at the End of the Line

Java string blocks introduced a new escape sequence to indicate the end of the line using backslash(\).

           String strHtmlContent = """
                        <!DOCTYPE html>
                        <html>
                            <head>
                                <!-- head definitions go here -->
                            </head>
                            <body>
                                <!-- the content goes here -->
                            </body>
                        </html>\
                         """;

Escape Sequence: \s

Another escape sequence you can use to format a text block is “\s”. Trailing spaces are removed from each line by default.

    package com.slb.java;

    public class StringTextBlockDemo {

        public static void main(String[] args) {
                        String text = """
                        one    a\s
                        two \s  a \s
                        three  a\s""";
            System.out.println(text);
        }

    }
com.slb.java.StringTextBlockDemo
one    a 
two    a  
three  a 

Process finished with exit code 0

Conclusion

Text blocks allow us to use multi line strings in java code, which helps us in improving readability of the code.

Checkout java 17 new features here.

Govind

For over 15 years, I have worked as a hands-on Java Architect and Senior Engineer, specializing in building and scaling high-performance, enterprise-level applications. My career has been focused primarily within the FinTech, Telecommunications, or E-commerce sector, where I’ve led teams in designing systems that handle millions of transactions per day.

Checkout my profile here : AUTHOR https://simplifiedlearningblog.com/author/

Related

Java Tags:Java Text Blocks

Post navigation

Previous Post: Java util date to String
Next Post: How to read Excel File using jxl

More Related Articles

Java Virtual Threads (Project Loom) in Real Enterprise Applications Java
Top 50 Java Coding Interview Questions and Answers (2026 Updated) Java
Is Java Dead? Is java dead, 2023 ? Java
Assertions in JUnit 5 Java
What is Rest API in java Java
Top 20 Spring Boot Interview Questions for Freshers (2026 Edition): The Ultimate Cheat Sheet Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • How to Handle Errors in Spring Boot REST APIs (2026 Guide): The Global Exception Handling Pattern
  • Practical System Design Examples using Spring Boot, Queues, and Caches (2026 Guide)
  • Spring Boot Application.properties vs. YAML: The 2026 Architect’s Verdict
  • Top 20 Spring Boot Interview Questions for Freshers (2026 Edition): The Ultimate Cheat Sheet
  • Spring MVC vs. Spring Boot in 2026: The Senior Architect’s Definitive Guide

Recent Comments

  1. Govind on Performance Principles of Software Architecture
  2. Gajanan Pise on Performance Principles of Software Architecture
Simplified Learning

Demystifying complex enterprise architecture for senior engineers. Practical guides on Java, Spring Boot, and Cloud Native systems.

Explore

  • Home
  • About Us
  • Author Profile: Govind
  • Contact Us

Legal

  • Privacy Policy
  • Terms and Conditions
  • Disclaimer
© 2026 Simplified Learning Blog. All rights reserved.
We use cookies to improve your experience and personalize ads. By continuing, you agree to our Privacy Policy and use of cookies.

Powered by PressBook Green WordPress theme