Shortening Code with Pattern Matching Flashcards

1
Q

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.

A
  • Java 16
  • pattern matching
  • if statment
  • instanceof operator
  • controlling program flow that only executes a section of code that meets certain criteria
  • used in conjuction with if statments for greater program control.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
void compareIntegers(Number number) {
    if (number instanceof Integer) {
        Integer data = (Integer) number;
        System.out.print(data.compareTo(5));
    }
}
A

The variable data in this example is referred to as the pattern variable. Notice that this code also avoids any potential ClassCastException because the cast operation is executed only if the implicit instanceof operator returns true.

void compareIntegers(Number number) {
    if (number instanceof Integer data) {
        System.out.print(data.compareTo(5));
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

> [!NOTE]
Reassigning Pattern Variables
While possible, it is a bad practice to reassign a pattern variable since doing so can lead to ambiguity about what is and is not in scope.
The reassignment can be prevented with a final modifier, but it is better not to reassign the variable at all.

A
if (number instanceof Integer data) {
    data = 10;
}
if (number instanceof final Integer data) {
    data = 10; // DOES NOT COMPILE
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Pattern Variables and Expressions

A

Pattern matching includes expressions that can be used to filter data out, such as in the following example:

void printIntegersGreaterThan5(Number number) {
    if (number instanceof Integer data && data.compareTo(5) > 0)
        System.out.print(data);
}

We can apply a number of filters, or patterns, so that the if statement is executed only in specific circumstances. Notice that we’re using the pattern variable in an expression in the same line in which it is declared.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Subtypes

A

The type of the pattern variable must be a subtype of the variable on the left side of the expression. It also cannot be the same type. This rule does not exist for traditional instanceof operator expressions, though.

Consider the following two uses of the instanceof operator:

Integer value = 123;
if (value instanceof Integer) {}
if (value instanceof Integer data) {} // DOES NOT COMPILE

While the second line compiles, the last line does not compile because pattern matching requires that the pattern variable type Integer be a strict subtype of Integer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

> [!NOTE]
Limitations of Subtype Enforcement
The compiler has some limitations on enforcing pattern matching types when we mix classes and interfaces, which will make more sense after you read Chapter 7, “Beyond Classes.” For example, given the non-final class Number and interface List, this does compile even though they are unrelated:

A
Number value = 123;
if (value instanceof List) {}
if (value instanceof List data) {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly