Monday, May 22, 2023

Quiz yourself: Java text blocks and escape sequences

Quiz Yourself, Oracle Java, Oracle Java Certification, Oracle Java Guides, Oracle Java Learning, Oracle Java Prep, Java Preparation

If a triple double quote sequence indicates the beginning and end of a text block, how do you assign a triple double quote sequence to a string?


Which of the following are valid Java text blocks? Choose three.

A. var v1 = """""";.

B. var v2 = """abc""";.

C. var v3 = """
         "
         """;

D. var v4 = """
         \ \s
         """;

E. var v5 = """
         \"
         """;

F. var v6 = """
         \"""
         """;

Answer. A Java text block starts with triple (three) double quote marks, and that character sequence must form the end of that line of source code. It’s permitted to have spaces or tabs before the actual newline character, but nothing else is permitted.

In view of this, option A is incorrect because the text block opening delimiter (""") has no newline character after it. Instead, it’s followed immediately by another triple double quote sequence.

You can see that option B is also incorrect because there are text characters (abc) on that line of code in addition to another triple double quote sequence.

Option C forms a valid text block that creates a string that contains a single double quote sequence followed by a newline character. Therefore, option C is correct.

One of the useful characteristics of Java’s text block feature is that it removes the special meaning of many characters, notably the double quote mark sequence. Because of this, it’s possible to write text far more cleanly. Consider trying to embed JavaScript or SQL code into a Java string; you’ll need double quote marks frequently, and in a regular form of string these would have to be “escaped.” However, with the text block, you can avoid escaping—unless you need three double quote marks in a row; then you’ll need to escape at least one of them.

Option D shows a single backslash followed by a space (and then another backslash followed by s).

A backslash must introduce an accepted escape sequence, and you are probably familiar with some of the most common ones: \n (line feed), \t (horizontal tab), and \\, which represents a single backslash in the resulting string. This sequence can be any one of those specified in the Java Language Specification, section 3.10.7.

However, this section expressly says the following:

It is a compile-time error if the character following a backslash in an escape sequence is not a Line Terminator or an ASCII b, s, t, n, f, r, ", ', \, 0, 1, 2, 3, 4, 5, 6, or 7.

In view of this, you can see that the backslash in option D, followed as it is by the space character, is an invalid combination; thus, option D is incorrect.

Option E is correct and demonstrates the long-standing approach for adding a double quote mark into a string. While this was the only way to achieve this prior to the text block, it’s not necessary to escape one (or two sequential) double quote marks in a text block. If you want three double quote marks in a row, you must escape at least one of them, or they will be taken as the end of the block. Notice that the string that results in option E will be identical to that of option C.

In addition, Option F is correct and shows one way to add three consecutive double quote marks inside a text block. As mentioned in the discussion about option E, you can escape the first, the second, or the third double quote mark—or, indeed, any combination of them. All these approaches are valid and identical, if there are not three unescaped double quote marks in sequence (which would terminate the block). Therefore, the following variants are also valid:

// good too
var v6 = """
         "\""
         """;

// the same as previous
var v6 = """
         \"\"\"
         """;

Conclusion. The correct answers are options C, E, and F.

Related Posts

0 comments:

Post a Comment