
- Java 14 Tutorial
- Java 14 - Home
- Java 14 - Overview
- Java 14 - Environment Setup
- Java 14 Language Changes
- Java 14 - Switch Expressions
- Java 14 - Text Blocks
- Java 14 - pattern for instanceOf
- Java 14 - NullPointerException
- Java 14 JVM Changes
- Java 14 - Packaging Tools
- Java 14 - NUMA Aware G1
- Java 14 - Others
- Java 14 - Deprecation & Removals
- Java 14 Useful Resources
- Java 14 - Quick Guide
- Java 14 - Useful Resources
- Java 14 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java 14 - 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 have following enhancements −
\ − indicates an end of the line in case new line is to be started.
\s − indicates a single space.
Example
Consider the following example −
ApiTester.java
public class APITester { public static void main(String[] args) { String stringJSON = "{\r\n" + "\"Name\" : \"Mahesh\"," + "\"RollNO\" : \"32\"\r\n" + "}"; System.out.println(stringJSON); String textBlockJSON = """ {"name" : "Mahesh", \ "RollNO" : "32" } """; 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 -Xlint:preview --enable-preview -source 14 APITester.java $java --enable-preview APITester
Output
{ "Name" : "Mahesh","RollNO" : "32" } { "name" : "Mahesh", "RollNO" : "32" } Contains: true indexOf: 15 Length: 45
Advertisements