8 Primitive Types in Patterns, instanceof, and switch
You can use primitive types in all pattern contexts, and the instanceof operator and switch statements and expressions work with all primitive types. See the following sections for more information:
- Type Patterns with Primitive Types
- Primitive Types in Record Patterns
- Primitive Values in switch Expressions and Statements
- Safe Casting with instanceof and switch: You can use the
instanceofoperator to test whether a cast is safe between any two types, and not only between two reference types. - When Clauses: You can use guards that restrict values of primitive types that come from a top-level pattern. A guard is a boolean expression in a
whenclause that follows a pattern label.
Note:
Using primitive types in patterns,instanceof, and switch is a preview feature. This is a preview
feature. A preview feature is a feature whose
design, specification, and implementation are
complete, but is not permanent. A preview feature
may exist in a different form or not at all in
future Java SE releases. To compile and run code
that contains preview features, you must specify
additional command-line options. See Preview Language and VM Features.See JEP 530: Primitive Types in Patterns, instanceof, and switch (Fourth Preview) for additional information.