OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Читать онлайн книгу.converts a
String
to an Integer
wrapper class.
All of the numeric classes in Table 1.7 extend the Number
class, which means they all come with some useful helper methods: byteValue()
, shortValue()
, intValue()
, longValue()
, floatValue()
, and doubleValue()
. The Boolean
and Character
wrapper classes include booleanValue()
and charValue()
, respectively.
As you probably guessed, these methods return the primitive value of a wrapper instance, in the type requested.
Double apple = Double.valueOf("200.99"); System.out.println(apple.byteValue()); // -56 System.out.println(apple.intValue()); // 200 System.out.println(apple.doubleValue()); // 200.99
These helper methods do their best to convert values but can result in a loss of precision. In the first example, there is no 200
in byte
, so it wraps around to -56
. In the second example, the value is truncated, which means all of the numbers after the decimal are dropped. In Chapter 5, we apply autoboxing and unboxing to show how easy Java makes it to work with primitive and wrapper values.
Some of the wrapper classes contain additional helper methods for working with numbers. You don't need to memorize these; you can assume any you are given are valid. For example, Integer
has:
max(int num1, int num2), which returns the largest of the two numbers
min(int num1, int num2), which returns the smallest of the two numbers
sum(int num1, int num2), which adds the two numbers
Defining Text Blocks
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?
14: String pyramid = """ 15: * 16: * * 17: * * * 18: """; 19: System.out.print(pyramid);
There are four lines of output. Lines 15–17 have stars. Line 18 is a line without any characters. 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
Formatting |
Meaning in regular String
|
Meaning in text block |
---|---|---|
\"
|
"
|
"
|
\"""
|
n/a – Invalid |
"""
|
\"\"\"
|
"""
|
"""
|
Space (at end of line) | Space | Ignored |
\s
|
Two spaces (\s is a space and preserves leading space on the line)
|
Two spaces |
\ (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"""