- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Java SE Pattern Matching for Enhanced Control Flow
This lab focuses on control-flow and branching logic. You’ll simplify complex decision-making—geometry shapes, validation logic, and legacy if/else chains—by refactoring to records, sealing the shape hierarchy for type safety, and replacing conditional checks with pattern matching in switch expressions.
Lab Info
Table of Contents
-
Challenge
Step 1: Getting Started
Welcome to Java SE Pattern Matching for Enhanced Control Flow. This lab modernizes a shape processing application using modern Java features: records, sealed interfaces, and pattern matching. The focus is on control-flow and branching logic—how you simplify complex decisions (geometry shapes, input validation paths, and legacy if/else chains) with concise pattern matching.
The starter code contains traditional Java classes with verbose boilerplate. By the end, the code will be concise, type-safe, and expressive. You’ll practice the same techniques you can use in other scenarios, like validating user input safely. > This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us.
-
Challenge
Step 2: Pattern Matching with instanceof
Modern
instanceoflets you bind a variable directly in the check, removing the cast that usually follows. You'll start by cleaning up the oldif/elseladder incalculateAreawith pattern variables. -
Challenge
Step 3: Converting Classes to Records
Java Records provide a compact syntax for immutable data classes. Each record automatically supplies a constructor, accessors, and
equals/hashCode/toString, replacing a lot of boilerplate. -
Challenge
Step 4: Creating a Sealed Shape Hierarchy
Sealed interfaces restrict which classes can implement them. By sealing
Shape, you tell the compiler exactly which shapes exist, enabling exhaustive pattern matching in switch expressions. -
Challenge
Step 5: Pattern Matching with Switch
Pattern matching for switch lets you switch directly on object types. Combined with a sealed interface, the compiler can verify you handled every shape. You'll refactor both methods in
ShapeProcessorto use concise, null-safe switch expressions. -
Challenge
Step 6: See It in Action
The refactoring is complete. The shape processor now uses:
- Records for concise, immutable data classes
- Sealed interfaces for a closed type hierarchy
- Pattern matching for elegant, type-safe processing
Run the demo to see the modernized code in action. Congratulations — you've finished the lab! By completing these steps you modernized a shape processor using three powerful Java features: records, sealed interfaces, and pattern matching. These changes eliminated boilerplate code, enabled compile-time type safety, and replaced verbose if-else chains with concise switch expressions.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.