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

Subtypes

Оглавление

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.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

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