Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 178

Pattern Variables and Expressions

Оглавление

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.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх