Skip to content
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

Simplified Learning Blog

Learning made easy

  • Java
    • Core Java Tutorial
    • Java 8
    • What is Rest API in java
    • Spring Framework
    • Type Casting in Java | 2 types Implicit and explicit casting
    • Spring Boot Tutorial
      • Spring Boot Rest API Example complete guide
    • Top 50 Java Interview Questions
    • JUnit 5 Tutorial
      • Assertall in JUnit 5
      • Assertions in JUnit 5
    • Java Thread Tutorials
      • How to create thread in Java
      • Multithreading in java
      • Daemon Thread in Java | How to create daemon thread in java
      • Top 40+ Multithreading interview questions
  • 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
    • Top 10 AWS Lambda interview questions
  • Java Codes
  • Software Architecture
    • Software Architecture Performance
    • Performance Principles of Software Architecture
    • System Performance Objective
  • Spring Boot Tutorial
  • Tools
    • JSON Formatter and Validator
  • 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 Admin 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.

Related

Java Tags:Java Text Blocks

Post navigation

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

Leave a Reply Cancel reply

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

Recent Posts

  • PDF to JSON Convertor
  • System Performance Objective
  • Performance Principles of Software Architecture
  • Calculate date of birth from age in jquery
  • How to convert excel to PDF using java

Recent Comments

No comments to show.

Copyright © 2025 Simplified Learning Blog.

Powered by PressBook WordPress theme