Skip to main content


Showing posts with the label jdk13

Java 13 - How to Concatenate Text Blocks

Introduction If you have not tried the Text Blocks feature introduced in Java 13 then the following post will help you to get started Java 13 - Example to use Text Blocks for Multi line String literals We can concatenate TextBlocks as we do with normal String as demonstrated in the following example. Example public class TextBlock { public static void main(String[] args) { String html = """             <html>                 <body>                  """ +                 """   <h1> Header One </h1>                 <p> Paragraph One </p>               """ +                 """                 <h1> Header One </h1>                 <p> Paragraph One </p>       """ +                """                 </body>              </html>                 """; S

Java 13 - Example to use Text Blocks for Multi line String literals

Introduction Lengthy Strings in Java code becomes hard to read. Multiline String literals can be XML, JSON, HTML, SQL queries, Hibernate or JPA queries, etc. Example String html = " <html>\r\n" + " <body>\r\n" + " <p>Hello, world</p>\r\n" + " </body>\r\n" + " </html>\r\n" ; Solution Thanks to the Text blocks feature introduced in Java 13. Now, these multiline String literals are more presentable in Java code. 1. Configuring Eclipse for Text Blocks 1.1 Requirments JDK 13 Eclipse Version: 2020-03 (4.15.0) 1.2 Eclipse Error 1 String literal is not properly closed by a double-quote 1.2.1 Solution Change the source of your program to 13 as shown in the following screenshot Java Source 13 1.3 Eclipse Error 2 Text Blocks is a preview feature and disabled by default. Use --enable-preview to enable 1.3.2 Eclipse Erro