The Java 17 release introduces Pattern Matching for switch as a preview language feature. Pattern matching allows case labels with patterns rather than just constants, which gives us more flexibility when defining conditions for switch cases.
- Type pattern
How to perform a different action for each type of object, without using instance of?
Now the switch selector expression can be of any type.
- Sealed class
Using pattern matching and sealed class, the compiler can check that all allowed subclasses are covered and there is no longer a need for a default case.
- Guarded pattern
Guarded pattern allow us to perform additional checks.
The full implementation of this tutorial can be found over on GitHub.