Earlier we saw a simple String with the value “hello”. What if we want to have a String with something more complicated? For example, let’s figure out how to create a String with this value:
“Java Study Guide”
by Scott & Jeanne
Building this as a String requires two things you haven’t learned yet. The syntax \” lets you say you want a ” rather than to end the String, and \n says you want a new line. Both of these are called escape characters because the backslash provides a special meaning. With these two new skills, we can write
String eyeTest = “\”Java Study Guide\”\n by Scott & Jeanne”;
While this does work, it is hard to read. Luckily, Java has text blocks, also known as multiline strings. See Figure 1.3 for the text block equivalent.
FIGURE 1. 3 Text block
A text block starts and ends with three double quotes (“””), and the contents don’t need to be escaped. This is much easier to read. Notice how the type is still String. This means the methods you learn about in Chapter 4 for String work for both a regular String and a text block.
You might have noticed the words incidental and essential whitespace in the figure. What’s that? Essential whitespace is part of your String and is important to you. Incidental whitespace just happens to be there to make the code easier to read. You can reformat your code and change the amount of incidental whitespace without any impact on your String value.
Imagine a vertical line drawn on the leftmost non-whitespace character in your text block. Everything to the left of it is incidental whitespace, and everything to the right is essential whitespace. Let’s try an example. How many lines does this output, and how many incidental and essential whitespace characters begin each line?
There are four lines of output. Lines 15–17 have stars. Line 18 is a line without any char-acters. The closing triple ” would have needed to be on line 17 if we didn’t want that blank line. There are no incidental whitespace characters here. The closing “”” on line 18 are the leftmost characters, so the line is drawn at the leftmost position. Line 15 has two essential whitespace characters to begin the line, and line 16 has one. That whitespace fills in the line drawn to match line 18.
Table 1.8 shows some special formatting sequences and compares how they work in a regular String and a text block.
TABLE 1. 8 Text block formatting
Meaning in | Meaning in | |
Formatting | regular String | text block |
\” | “ | “ |
\””” | n/a – Invalid | “”” |
\”\”\” | “”” | “”” |
Space (at end of line) | Space | Ignored |
\s | Two spaces (\s is a space and preserves | Two spaces |
leading space on the line) | ||
\ (at end of line) | n/a – Invalid | Omits new line on |
that line | ||
Let’s try a few examples. First, do you see why this doesn’t compile?
String block = “””doe“””; // DOES NOT COMPILE
Text blocks require a line break after the opening “””, making this one invalid. Now let’s try a valid one. How many lines do you think are in this text block?
String block = “””
doe \
deer“””;
Just one. The output is doe deer since the \ tells Java not to add a new line before deer.
Let’s try determining the number of lines in another text block:
String block = “””
doe \n
deer
“””;
This time we have four lines. Since the text block has the closing “”” on a separate line, we have three lines for the lines in the text block plus the explicit \n. Let’s try one more. What do you think this outputs?
String block = “””
“doe\”\”\”
\”deer\”””
“””;
System.out.print(“*”+ block + “*”);
The answer is
* “doe”””
“deer”””
*
All of the \” escape the “. There is one space of essential whitespace on the doe and deer lines. All the other leading whitespace is incidental whitespace.