OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky

Читать онлайн книгу.

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

      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";

Schematic illustration of 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.

      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.

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"""


Скачать книгу