Featured resource
2026 Tech Forecast
2026 Tech Forecast

Stay ahead of what’s next in tech with predictions from 1,500+ business leaders, insiders, and Pluralsight Authors.

Get these insights
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Core Tech
Labs

Guided: 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 platform
Lab Info
Level
Intermediate
Last updated
Dec 05, 2025
Duration
30m

Contact sales

By clicking submit, you agree to our Privacy Policy and Terms of Use, and consent to receive marketing emails from Pluralsight.
Table of Contents
  1. 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.

  2. Challenge

    Step 2: Pattern Matching with instanceof

    Modern instanceof lets you bind a variable directly in the check, removing the cast that usually follows. You'll start by cleaning up the old if/else ladder in calculateArea with pattern variables.

  3. 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.

  4. 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.

  5. 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 ShapeProcessor to use concise, null-safe switch expressions.

  6. 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

Pluralsight Code Labs offer an opportunity to get hands-on learning in real-time. Be it a Challenge, Sandbox, or Guided Lab, these provide the real world experience needed to succeed in your development role.

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.

Get started with Pluralsight