Pattern Matching for Switch in Java 21
41mIntermediate2025-06-20
Authors

Buddhini Samarakkody
Java Engineer, Author, Instructor
Course details
The course is designed for intermediate Java developers looking to upskill and grow their career. Join instructor Buddhini Samarakkody as she covers the essentials of the pattern matching feature for switch statements and expressions which was introduced in Java 21. Learn how patterns can be used in case labels of a switch statement or expression, using guarded patterns to further test if a value matches a pattern, the use of Java Records in switch, along with pattern matching. Along the way, Buddhini outlines the use of the null case label to avoid NullPointerExceptions in switch statements or expressions. By the end of this course, you’ll be equipped with the skills you need to write more readable, more concise, more powerful code.
Learning objectives
Identify the need for pattern matching for switch.
Write switch statements or expressions that contain patterns in case labels.
Demonstrate how to use null case labels in a switch statement or expression to prevent NullPointerExceptions.
Write switch statements or expressions that contain guarded patterns in case labels to further test if a value matches a pattern.
Demonstrate the use of switch statements and expressions that use Java Records for pattern
Learning objectives
Identify the need for pattern matching for switch.
Write switch statements or expressions that contain patterns in case labels.
Demonstrate how to use null case labels in a switch statement or expression to prevent NullPointerExceptions.
Write switch statements or expressions that contain guarded patterns in case labels to further test if a value matches a pattern.
Demonstrate the use of switch statements and expressions that use Java Records for pattern
Skills covered
JavaOracleProgramming LanguagesSoftware DevelopmentOne-Off
Concepts
0. Introduction
- 01 - Learning about pattern matching for switch
- 02 - What you should know
- 03 - Tour of CoderPad
1. The Background to Pattern Matching
- 04 - What is pattern matching
- 05 - Motivation - Pattern matching for instanceof
- 06 - Evolution of switch in Java
2. Pattern Matching for Switch
- 07 - Patterns in case labels
- 08 - Using patterns in case labels
- 09 - The null case label
- 10 - Using the null case label
- 11 - Guarded pattern case labels
- 12 - Using guarded pattern case labels
3. Further Considerations
- 13 - Ordering case labels
- 14 - Exhaustiveness of a switch block
- 15 - Using record patterns with switch
Conclusion
- 16 - Next steps
Related courses
Related learn paths
- C# Excellence: Architecting High-Performance Solutions
- Unlock Your Career Potential: Use LinkedIn to Build Confidence and Career Skills
- Develop Your Scala Skills for Data Engineering
- Prepare for the AWS DevOps Engineer - Professional (DOP-C01) Exam
- Become an AWS Data and DevOps Specialist
- Getting Started with Microservices
- Microservices Foundations Professional Certificate by Kong
- Advance Your PHP Skills