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

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


Скачать книгу
allowing the application to selectively execute particular segments of code.

      These statements can be applied to single expressions as well as a block of Java code. As described in Chapter 1, a block of code in Java is a group of zero or more statements between balanced braces ({}) and can be used anywhere a single statement is allowed. For example, the following two snippets are equivalent, with the first being a single expression and the second being a block containing the same statement:

      A statement or block often serves as the target of a decision-making statement. For example, we can prepend the decision-making if statement to these two examples:

      // Single statement if(ticketsTaken> 1) patrons++; // Statement inside a block if(ticketsTaken> 1) { patrons++; }

      Again, both of these code snippets are equivalent. Just remember that the target of a decision-making statement can be a single statement or block of statements. For the rest of the chapter, we use both forms to better prepare you for what you will see on the exam.

      Note Icon While both of the previous examples are equivalent, stylistically using blocks is often preferred, even if the block has only one statement. The second form has the advantage that you can quickly insert new lines of code into the block, without modifying the surrounding structure.

      The if Statement

      For example, imagine we had a function that used the hour of day, an integer value from 0 to 23, to display a message to the user:

      if(hourOfDay < 11) System.out.println("Good Morning");

      If the hour of the day is less than 11, then the message will be displayed. Now let's say we also wanted to increment some value, morningGreetingCount, every time the greeting is printed. We could write the if statement twice, but luckily Java offers us a more natural approach using a block:

      if(hourOfDay < 11) { System.out.println("Good Morning"); morningGreetingCount++; }

      Watch Indentation and Braces

      One area where the exam writers will try to trip you up is if statements without braces ({}). For example, take a look at this slightly modified form of our example:

       if(hourOfDay < 11) System.out.println("Good Morning"); morningGreetingCount++;

      Based on the indentation, you might be inclined to think the variable morningGreetingCount is only going to be incremented if hourOfDay is less than 11, but that's not what this code does. It will execute the print statement only if the condition is met, but it will always execute the increment operation.

      Remember that in Java, unlike some other programming languages, tabs are just whitespace and are not evaluated as part of the execution. When you see a control flow statement in a question, be sure to trace the open and close braces of the block, ignoring any indentation you may come across.

      The else Statement

      Let's expand our example a little. What if we want to display a different message if it is 11 a.m. or later? Can we do it using only the tools we have? Of course we can!

Schematic illustration of the structure of an else statement

      Let's return to this example:

      if(hourOfDay < 11) { System.out.println("Good Morning"); } else System.out.println("Good Afternoon");

      Now our code is truly branching between one of the two possible options, with the boolean evaluation happening only once. The else operator takes a statement or block of statements, in the same manner as the if statement. Similarly, we can append additional if statements to an else block to arrive at a more refined example:

      if(hourOfDay < 11) { System.out.println("Good Morning"); } else if(hourOfDay < 15) { System.out.println("Good Afternoon"); } else { System.out.println("Good Evening"); }

      Verifying That the if Statement Evaluates to a Boolean Expression

      Another common way the exam may try to lead you astray is by providing code where the boolean expression inside the if statement is not actually a boolean expression. For example, take a look at the following lines of code:

       int hourOfDay = 1; if(hourOfDay) { // DOES NOT COMPILE … }

      This statement may be valid in some other programming and scripting languages, but not in Java, where 0 and 1 are not considered boolean values.

      Shortening Code with Pattern Matching

      Java 16 officially introduced pattern matching with if statements and the instanceof operator. Pattern matching is a technique of controlling program flow that only executes a section of code that meets certain criteria. It is used in conjunction with if statements for greater program control.

      Note Icon If pattern matching is new to you, be careful not to confuse it with the Java Pattern class or regular expressions (regex). While pattern matching can include the use of regular expressions for filtering, they are unrelated concepts.

      Pattern matching is a new tool at your disposal to reduce boilerplate in your code. Boilerplate code is code that tends to be duplicated throughout a section of code over and over again in a similar manner. A lot of the newer enhancements to the Java language focus on reducing boilerplate code.

      To understand why this tool was added, consider the following code that takes a Number instance and compares it with the value 5. If you haven't seen Number or Integer,


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