instanceof
The Java operator that checks whether a value is an instance of a type, and with a pattern also binds it to a typed variable.
In more detail
Older code wrote the check and then a separate cast. Pattern matching folds the two together, so value instanceof String text gives you text as a String inside the true branch.
Example
value instanceof String textRelated terms
- pattern matching — Testing a value against a shape, such as a type, a range or the values of its properties, and optionally extracting parts of it in the same step.
- type cast — An instruction to treat a value as a more specific type, written (String) value, which fails at runtime if the value is not that type.
Taught in
- Pattern Matching And Text Blocks — Modern Java: Virtual Threads & High-Throughput Services
- Sealed Types, Casts And Pattern Matching — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Generics, Bounds And Wildcards — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep