Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Java Coding Problems

You're reading from   Java Coding Problems Become an expert Java programmer by solving over 250 brand-new, modern, real-world problems

Arrow left icon
Product type Paperback
Published in Mar 2024
Publisher Packt
ISBN-13 9781837633944
Length 798 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Anghel Leonard Anghel Leonard
Author Profile Icon Anghel Leonard
Anghel Leonard
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Text Blocks, Locales, Numbers, and Math 2. Objects, Immutability, Switch Expressions, and Pattern Matching FREE CHAPTER 3. Working with Date and Time 4. Records and Record Patterns 5. Arrays, Collections, and Data Structures 6. Java I/O: Context-Specific Deserialization Filters 7. Foreign (Function) Memory API 8. Sealed and Hidden Classes 9. Functional Style Programming – Extending APIs 10. Concurrency – Virtual Threads and Structured Concurrency 11. Concurrency ‒ Virtual Threads and Structured Concurrency: Diving Deeper 12. Garbage Collectors and Dynamic CDS Archives 13. Socket API and Simple Web Server 14. Other Books You May Enjoy
15. Index

2. Exemplifying the usage of text block delimiters

Remember from the previous problem, Creating a multiline SQL, JSON, and HTML string, that a text block is syntactically delimited by an opening and a closing delimiter, represented by three double quotation marks, """.

The best approach for exemplifying the usage of these delimiters consists of three simple steps: consider an example, inspect the output, and provide a conclusion. This being said, let’s start with an example that imitates some of the JEP’s examples:

String sql= """
            UPDATE "public"."office"
            SET ("address_first", "address_second", "phone") =
              (SELECT "public"."employee"."first_name",
                      "public"."employee"."last_name", ?
               FROM "public"."employee"
               WHERE "public"."employee"."job_title" = ?)""";

So by following the JEP examples, we have to align the content with the opening delimiter. It’s probable that this alignment style is not consistent with the rest of our code and is not such a good practice. What will happen with the text block content if we rename the sql variable updateSql, updateOfficeByEmployeeJobTitle, or something else? Obviously, in order to preserve the alignment, this will push our content to the right even more. Fortunately, we can shift-left the content without affecting the final result, as follows:

String sql = """
  UPDATE "public"."office"
  SET ("address_first", "address_second", "phone") =
    (SELECT "public"."employee"."first_name",
            "public"."employee"."last_name", ?
     FROM "public"."employee"
     WHERE "public"."employee"."job_title" = ?""";

Shifting right the opening/closing delimiters themselves will not affect the resulting String. It is unlikely that you’ll have a good reason to do this, but just for the sake of completion, the following example produces the same result as the previous two examples:

String sql =                                         """
  UPDATE "public"."office"
  SET ("address_first", "address_second", "phone") =
    (SELECT "public"."employee"."first_name",
            "public"."employee"."last_name", ?
     FROM "public"."employee"
     WHERE "public"."employee"."job_title" = ?       """;

Now, let’s see something more interesting. The opening delimiter doesn’t accept content on the same line, while the closing delimiter sits to the right at the end of the content. However, what happens if we move the closing delimiter to its own line, as in the following two examples?

String sql= """ 
            UPDATE "public"."office"
            SET ("address_first", "address_second", "phone") =
              (SELECT "public"."employee"."first_name",
                      "public"."employee"."last_name", ?
               FROM "public"."employee"
               WHERE "public"."employee"."job_title" = ?
            """;
String sql= """ 
  UPDATE "public"."office"
  SET ("address_first", "address_second", "phone") =
    (SELECT "public"."employee"."first_name",
            "public"."employee"."last_name", ?
     FROM "public"."employee"
     WHERE "public"."employee"."job_title" = ?
  """;

This time, the resulting string contains a new line at the end of the content. Check the following figure (the text -- BEFORE TEXT BLOCK – and -- AFTER TEXT BLOCK -- are just guidelines added via System.out.println() to help you delimit the text block itself; they are not necessary and not part of the text block):

Figure 1.1.png

Figure 1.1: Move the closing delimiter to its own line, vertically aligned with the opening delimiter

In the left figure (A) the closing delimiter is at the end of the content. However, in the right figure (B), we moved the closing delimiter to its own line, and as you can see, the resulting String was enriched with a new line at the end.

Important note

Placing the closing delimiter on its own line will append a new line to the final String. Also, pay attention that vertically aligning the opening delimiter, the content, and the closing delimiter to the left margin may result later in extra work. If the variable name is modified, then manual re-indentation is needed to maintain this alignment.

So pay attention to how you place the closing delimiter.

Do you find this weird? Well, that’s not all! In the previous example, the closing delimiter was placed on its own line but vertically aligned with the opening delimiter. Let’s take a step forward and let’s shift-left the end delimiter, as in the following example:

String sql= """ 
            UPDATE "public"."office"
            SET ("address_first", "address_second", "phone") =
              (SELECT "public"."employee"."first_name",
                      "public"."employee"."last_name", ?
               FROM "public"."employee"
               WHERE "public"."employee"."job_title" = ?
""";

The following figure reveals the effect of this action:

Figure 1.2.png

Figure 1.2: Moving the closing delimiter to its own line and shifting it to the left

In the left figure (A), we have the closing delimiter on its own line and aligned with the opening delimiter. In the right figure (B), we have the effect of the previous code. Moving the closing delimiter to the left results in an additional indentation of the content to the right. The additional indentation depends on how much we shift-left the closing delimiter.

Important note

Placing the closing delimiter on its own line and shifting it to the left will append a new line and additional indentation to the final String.

On the other hand, if we move the closing delimiter to its own line and shift it to the right, it doesn’t affect the final String:

String sql= """ 
            UPDATE "public"."office"
            SET ("address_first", "address_second", "phone") =
              (SELECT "public"."employee"."first_name",
                      "public"."employee"."last_name", ?
               FROM "public"."employee"
               WHERE "public"."employee"."job_title" = ?
                                              """;

This code appends a new line to the final String but doesn’t affect indentation. In order to better understand the behavior of opening/closing delimiters, you have to explore the next problem.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime