Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java Interfaces

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Class References

Java Useful Resources

Java - Text Blocks



Java made text blocks in Java 15 as a standard feature to handle multiline strings like JSON/XML/HTML etc. It was introduced in Java 13 as a preview feature.

Purpose of introducing text block is mainly to declare multi-line strings most efficiently. Prior to text block, we can declare multi-line strings using string concatenation, string builder append method, string join method but that approach is quite messy. As we have to use line terminators, delimiters etc to mark a new line. Text block provides a better and alternate approach to define multiline string using a """, 3 double-quotes mark.

Text Block Syntax

A text block is an enhancement to existing String object with special syntax where string content should starts with """ with newline and ends with """. Any content within """ will be used as-is.

String textBlockJSON = """
   {
      "name" : "Mahesh",
      "RollNO" : "32"
   }
   """;

Equivalent String can be written using older syntax as shown below:

String stringJSON = "{\r\n" 
         + "   \"Name\" : \"Mahesh\",\r\n" 
         + "   \"RollNO\" : \"32\"\r\n" 
         + "}";  

Example of Java Text Block

In this example, we've printed the json string using text block as well as using string concatenation.

package com.tutorialspoint;

public class Tester {

   public static void main(String[] args) {
      String stringJSON = "{\r\n" 
      + "   \"Name\" : \"Mahesh\",\r\n" 
      + "   \"RollNO\" : \"32\"\r\n" 
      + "}";  

      System.out.println(stringJSON);

      String textBlockJSON = """
      {
         "name" : "Mahesh",
         "RollNO" : "32"
      }
      """;
      System.out.println(textBlockJSON);
   }   
}

Output

Let us compile and run the above program, this will produce the following result −

{
   "Name" : "Mahesh",
   "RollNO" : "32"
}
{
   "name" : "Mahesh",
   "RollNO" : "32"
}

Text Block String Operations

Text block is same as String and can be compared using equals() method or equal operator.

// compare the content, 
textBlockJSON.equals(stringJSON);
	      
// compare the objects
textBlockJSON == stringJSON;

Text block supports all string operations like indexOf(), contains() etc.

// check if text block contains a provided string or not
textBlockJSON.contains("Mahesh");

// get the length of string content
textBlockJSON.length()

Example: Text Block String Operations in Java

In this example, we've performed various string operations and compared text block with an equivalent string.

package com.tutorialspoint;

public class Tester {

   public static void main(String[] args) {
      String stringJSON = "Mahesh";

      String textBlockJSON = """
      Mahesh""";
      // compare the content
      System.out.println(textBlockJSON.equals(stringJSON));

      // compare the objects
      System.out.println(textBlockJSON == stringJSON);

      // text block supports all string operations
      System.out.println("Contains: " + textBlockJSON.contains("Mahesh"));
      System.out.println("indexOf: " + textBlockJSON.indexOf("Mahesh"));
      System.out.println("Length: " + textBlockJSON.length());
   }   
}

Output

Let us compile and run the above program, this will produce the following result −

true
true
Contains: true
indexOf: 0
Length: 6

Text Block Methods

  • stripIndent() - removes incidental white spaces from the start and end of the string.

  • translateEscapes() - translate the escape sequences as per the string syntax.

  • formatted() - similar to String format() method to support formatting in text block strings.

Example

Consider the following example −

ApiTester.java

public class APITester {

   public static void main(String[] args) {
	  String textBlockJSON = """
         {
            "name" : "%s",
            "RollNO" : "%s"
         }
         """.formatted("Mahesh", "32");
      System.out.println(textBlockJSON);
   }   
}

Output

{
   "name" : "Mahesh",
   "RollNO" : "32"
}
Advertisements