Monday, April 25, 2022

Quiz yourself: Java’s text blocks and variable-length argument lists

When do text blocks contain a leading new line, and when won’t you see a new line?

[Java 17, which is a long-term support release, is a significant step forward from Java 11. Although Java 17 became generally available in September 2021, the matching version of the certification exam has not been released as of this quiz’s publication date. The exam is in the later stages of development, and we’re excited to begin presenting questions based on the objectives for the new exam. —Ed.]

Oracle Java Certification, Oracle Java Career, Java Jobs, Java Skills, Oracle Java Preparation, Oracle Java Exam Preparation

Given the following method code fragment

var f = """

        ◌◌%s

        """.concat("◌◌").stripIndent();

f.concat("""

         ◌◌%s

         """);

System.out.print(f.formatted(new String[]{"A", "B"}));

Assume each dotted circle (◌) denotes a space character.

What is the result? Choose one.

A. A

B. ◌◌A

C. ◌◌A

     ◌◌B

D. A

    ◌◌B

E. [Ljava.lang.String;@41629346

Answer. This question investigates text blocks, new String behaviors, and the handling of variable-length argument lists. The investigation begins with the preparation of a String that starts out as a literal text block; keep in mind that the ◌ character in this quiz denotes a space. The string literal starts out as the following:

◌◌%s<new line>

Notice there’s no leading new line there; the text block rules mandate that the three opening double quote marks must be followed by a new line. The contents of the text block actually start on that second line.

Immediately after the text block, there’s a concat() method that creates a new String containing the following:

◌◌%s<new line>

◌◌

The next operation is the stripIndent() method. This method deletes the indentation that’s common across all the lines of a String. In this code sample, there are two spaces on each line, and these will be removed. Therefore, the String assigned to the variable f is

%s<new line>

The second concat() operation appends a new text block to f and produces a new string—but that String is lost because the result of the operation is not stored. Remember, it’s fundamental to String objects that they are immutable.

Java 15 added a new utility method, s.formatted(...), which works the same way as String.format(s, ...). Notably, both of these methods, and also printf methods, take a variable-length argument list. In Java, a variable-length argument list is always passed as an array. That array can be built by the compiler from comma-separated arguments (which is how the arguments are normally presented), or the array can be provided by the programmer. For example, the following two lines are identical in their behavior:

System.out.print(f.formatted(new String[]{"A", "B"}));

System.out.print(f.formatted("A", "B"));

◉ The array argument passed to the formatted method effectively carries two values: A and B. Option E is incorrect because the array itself will not be presented in the output.

◉ Only one %s placeholder (sometimes called a format specifier) exists in the formatting string. Therefore, only the first of the two values will be used; the second element is simply ignored. Thus, the output contains A but not B.

◉ Because the spaces were deleted, the output will simply be the following:

A<new line>

These findings mean option A is correct, and options B, C, D, and E are incorrect.

Conclusion. The correct answer is option A.

Source: oracle.com

Related Posts

0 comments:

Post a Comment