0% found this document useful (0 votes)
13 views9 pages

Java21 Pattern Matching For Switch

Java 21 finalizes the pattern matching feature for switch statements, which was first introduced in Java 17. This feature allows for case labels to include patterns, null, and optional when clauses, broadening the selector expression types to include any reference type. Additionally, improvements have been made to enum constant case labels and the concept of label dominance is introduced to manage unreachable code.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views9 pages

Java21 Pattern Matching For Switch

Java 21 finalizes the pattern matching feature for switch statements, which was first introduced in Java 17. This feature allows for case labels to include patterns, null, and optional when clauses, broadening the selector expression types to include any reference type. Additionally, improvements have been made to enum constant case labels and the concept of label dominance is introduced to manage unreachable code.

Uploaded by

gs23133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Java 21

Pattern Matching for ‘switch’

Copyright © Seán Kennedy


Pattern-matching for switch
• Pattern matching for switch statements and expressions was first
introduced as a preview feature in Java 17.

• Now, Java 21 finalizes the feature.

• Motivation – switch is a very natural fit for pattern matching.


Recall that pattern matching removes the need for the
instanceof-and-cast idiom.

• Other changes, such as the when clause, were motivated


by the desire to separate the case labels, patterns and
conditional logic from the business logic.
2

Copyright © Seán Kennedy


Pattern-matching for switch
• In other words, the selection of which branch to execute is
separated from what to do when we execute that branch.

• Changes include:
• case labels can include patterns and null
• case labels can include optional when clauses (“guards”)
• selector expression types broadened
• from:
• integral primitive types (excluding long), their corresponding wrapper
types, String and enums.
• to:
• integral primitive types (excluding long) and any reference type.
• enum constant case labels improved
• qualified enum constants now allowed
3

Copyright © Seán Kennedy


Pattern-matching for switch - pattern labels, null and when clauses

Copyright © Seán Kennedy


Pattern-matching for switch - selector expression types broadened

Copyright © Seán Kennedy


Pattern-matching for switch – enum constant case labels improved

Copyright © Seán Kennedy


Pattern-matching for switch
• Label “dominance”
• analogous to the catch clauses in a try statement
• unreachable code (label)

Copyright © Seán Kennedy


Pattern-matching for switch
• Label “dominance”
• unconditional pattern and default

Copyright © Seán Kennedy


Pattern-matching for switch
• Exhaustiveness

Copyright © Seán Kennedy

You might also like