- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: Apply Records and Pattern Matching in Java Services
In this Code Lab, you'll step into the role of a developer tasked with modernizing a Java application. You will take verbose, boilerplate-heavy classes and refactor them into clean, immutable records. You'll also simplify complex conditional logic using the latest pattern matching features for both `instanceof` and `switch` statements, making the code more readable and robust.
Lab Info
Table of Contents
-
Challenge
Introduction
Welcome to this Code Lab on modern Java features! You'll explore how records and pattern matching can significantly reduce boilerplate and improve the clarity and safety of your code. You'll start with a traditional Java application and refactor it step by step.
Before you dive into writing new code, familiarize yourself with the project structure. You're working with a standard Maven project where the main logic lives in
src/main/javaand tests are insrc/test/java. Explore the files, especiallyLegacyBook.java, which you'll refactor first, andMediaService.java, where you'll implement pattern matching.Navigation Tip: For easier code navigation, collapse the Workspace view in the Explorer pane and expand the Projects view instead. The Projects view organizes your Java files by package structure, making it much simpler to locate classes and interfaces.
You'll see two main projects:
- LAB-CODE: Your working code that you'll modify throughout the exercises
- SOLUTION: Reference implementations for each step (files follow the pattern
FILENAME-STEP#-TASK#.java, e.g.,Book-2-1.java)
Start by expanding LAB-CODE to see the source code you'll be working with. If you get stuck on any task, you can reference the corresponding solution file for guidance.
info> 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: From Verbose Classes to Concise Records
A common task in Java is creating classes to simply hold data, like results from a database query or a network request. Historically, this meant writing a lot of boilerplate: private final fields, a constructor, accessor methods, and
equals(),hashCode(), andtoString()implementations. These classes are often called Plain Old Java Objects (POJOs) or data transfer objects (DTOs).Java records, introduced as a standard feature in Java 16, are designed to eliminate this ceremony. They are a special, more restricted kind of class, optimized for the role of being a simple, immutable data aggregate.
-
Challenge
Step 3: Building a Data Model with Records
Now that you understand the basics of records, build out your data model. By using records for all your data-carrying objects, you ensure they are simple, immutable, and consistent. This also sets you up perfectly for using pattern matching in the next steps.
You'll also use a
sealed interfacenamedMedia. A sealed interface restricts which other classes or interfaces may extend or implement it. This gives the compiler a complete picture of the type hierarchy, which is essential for exhaustive checks in pattern matching. -
Challenge
Step 4: Simplifying `instanceof` Checks
A very common code pattern in Java is checking if an object is an instance of a specific type, and if so, casting it to that type to call one of its methods. This pattern is verbose and contains a redundancy: the type is mentioned twice.
if (obj instanceof String) { String s = (String) obj; System.out.println(s.length()); }Pattern matching for
instanceof(a standard feature since Java 16) streamlines this. It lets you declare a new variable of the target type, which is only in scope if the check succeeds. This makes the code more concise and removes the need for the manual cast. -
Challenge
Step 5: Advanced Data Handling with `switch` Expressions
The final feature you'll explore is pattern matching for
switchexpressions, made standard in Java 21. This feature elevatesswitchfrom a simple control flow statement to a powerful tool for data-oriented programming. You canswitchon an object and havecaselabels that match against types, not just constant values.When you use this with records and sealed interfaces, it allows for exhaustive and elegant handling of different data types. The compiler can verify that you've handled all permitted subtypes of a sealed interface, eliminating a whole class of runtime errors. -----
Congratulations on completing the lab! You've successfully modernized a Java application using records and pattern matching.
You've learned how to:
- Replace verbose POJOs with concise
recordtypes for immutable data. - Simplify
instanceofchecks and casts using pattern matching. - Rewrite cumbersome
if-elsechains as elegant and type-safeswitchexpressions.
These modern Java features help you write code that is not only shorter but also more readable, robust, and a pleasure to maintain.
- Replace verbose POJOs with concise
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.