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: Type Inference and Intro to Pattern Matching

In this Code Lab, you will refactor a simple Java media library application to use modern features like the 'var' keyword and pattern matching for 'instanceof'. This will make the code more concise, readable, and less prone to errors.

Lab platform
Lab Info
Level
Beginner
Last updated
Dec 06, 2025
Duration
45m

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

    Introduction

    Welcome to this Code Lab on modernizing Java! You'll take an existing application written with older Java syntax and refactor it using two powerful features: Local-Variable Type Inference (var) and Pattern Matching for instanceof. These features help make your code less verbose, more readable, and less prone to common errors like casting mistakes.

    You have a simple media library application. It has a MediaProcessor that processes a list of Media objects, which can be either a Book or a Movie. Look at the files, especially MediaProcessor.java. Notice the classic instanceof checks followed by explicit casts. Your goal is to clean this up.

    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.

  2. Challenge

    Step 2: Refactoring with the 'var' Keyword

    Use the var keyword to let the Java compiler infer the type of a local variable from its initializer. This is purely a compile-time feature that can significantly reduce boilerplate code, especially when dealing with long, complex type names like nested generics.

  3. Challenge

    Step 3: Simplifying Logic with 'instanceof' Pattern Matching

    Pattern matching for instanceof streamlines a common coding pattern: checking if an object is of a certain type, and if so, casting it and using it. Use the new syntax to test, bind the result to a new variable of the correct type, and use it, all in one go. The new pattern variable is only in scope where it's guaranteed to have been assigned, making your code safer.

  4. Challenge

    Step 4: Applying Pattern Matching in Domain Models

    The equals() method is a perfect candidate for refactoring with pattern matching. The old way requires several lines of boilerplate to safely check the type and cast. Simplify it to a single, expressive line.

  5. Challenge

    Step 5: Conclusion

    Congratulations on completing the Code Lab! You've successfully used var and instanceof pattern matching to modernize a Java application. Compare the final code to what you started with. It's cleaner, more concise, and easier to understand. Apply these powerful features to your own Java projects.

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