{"id":1456,"date":"2021-06-21T08:27:50","date_gmt":"2021-06-20T20:27:50","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1456"},"modified":"2021-09-28T13:58:54","modified_gmt":"2021-09-28T00:58:54","slug":"java-text-blocks","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1456","title":{"rendered":"Java Text Blocks"},"content":{"rendered":"<p>The text blocks is Java's way to simplify rendering of a string that spans multiple lines. A text block begins with three double-quote characters followed by a line terminator.<a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/15\/text-blocks\/index.html\">^1<\/a><\/p>\n<p>Example: <\/p>\n<pre><code class=\"language-java\">String text = &quot;&quot;&quot;\n    The quick \n    brown fox \n    jumps over \n    the lazy dog&quot;&quot;&quot;;<\/code><\/pre>\n<p>The above example is equivalent to:<\/p>\n<pre><code class=\"language-java\">String text = &quot;The quick \\n&quot;\n    + &quot;brown fox \\n&quot; \n    + &quot;jumps over \\n&quot;\n    + &quot;the lazy dog&quot;;<\/code><\/pre>\n<p>Notice who how simple the example against its equivalent.<\/p>\n<h2>Incidental and essential white space<\/h2>\n<p>The java text blocks differentiates the <strong>incidental white space<\/strong> from <strong>essential white space<\/strong><a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/15\/text-blocks\/index.html\">^1<\/a>. Like the following example:<\/p>\n<pre><code class=\"language-java\">void writeHTML() {\n    String html = &quot;&quot;&quot;\n\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7&lt;html&gt;\n\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7    &lt;body&gt;\n\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7        &lt;p&gt;Hello World.&lt;\/p&gt;\n\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7    &lt;\/body&gt;\n\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7&lt;\/html&gt;\n\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7\u00b7&quot;&quot;&quot;;\n    writeOutput(html);\n}<\/code><\/pre>\n<p>The dots, preceding the <html> tag represents the incidental white spaces while the space preceding the <body> tag not including the dots, represents the essential white spaces. <\/p>\n<p>Normally the left incidental white space can be controlled by the location of the ending delimiter of the text blocks.<\/p>\n<p>Trailing white space on each line in a text block is also considered incidental and is stripped away by the Java compiler<a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/15\/text-blocks\/index.html\">^1<\/a>.<\/p>\n<h2>Removing the ending new line of each line<\/h2>\n<p>Using the backslash at the end of each line will remove the implicit \\n character.<\/p>\n<pre><code class=\"language-java\">String text = &quot;&quot;&quot;\n    The quick \\\n    brown fox \\\n    jumps over \\\n    the lazy dog&quot;&quot;&quot;;<\/code><\/pre>\n<p>If you output the preceding variable it will become a single line like the following:<\/p>\n<pre><code>The quick brown fox jumps over the lazy dog<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The text blocks is Java&#8217;s way to simplify rendering of a string that spans multiple lines. A text block begins with three double-quote characters followed by a line terminator.^1 Example: String text = &quot;&quot;&quot; The quick brown fox jumps over the lazy dog&quot;&quot;&quot;; The above example is equivalent to: String text = &quot;The quick \\n&quot; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[68,71],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1456"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1456"}],"version-history":[{"count":1,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1456\/revisions"}],"predecessor-version":[{"id":1457,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1456\/revisions\/1457"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1456"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}