Java 15

Java 15 – Text Blocks

Java 13 introduces text blocks to handle multiline strings like JSON/XML/HTML etc as is a preview feature. With Java 14, we’ve second preview of Text Blocks. Now Text Block is no more a preview feature and is a part of standard offering.

Example

Consider the following example −

Java15Tester.java

public class Java15Tester {
   public static void main(String[] args) {
      String stringJSON = "{\r\n" 
         + "\"Name\" : \"Mahesh\"," 
         + "\"RollNO\" : \"35\"\r\n" 
         + "}";  
      System.out.println(stringJSON);
	   String textBlockJSON = """{"name" : "Mahesh", \"RollNO" : "35"}""";
      System.out.println(textBlockJSON);
	   System.out.println("Contains: " + textBlockJSON.contains("Mahesh"));
	   System.out.println("indexOf: " + textBlockJSON.indexOf("Mahesh"));
	   System.out.println("Length: " + textBlockJSON.length());
   }
}

Compile and Run the program

$javac Java15Tester.java
$java Java15Tester 

Output

{
   "Name" : "Mahesh","RollNO" : "35"
}
{
   "name" : "Mahesh",   "RollNO" : "35"
}
Contains: true
indexOf: 15
Length: 45

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs