What’s New in Java 21

What’s New in Java 21

Duration: 4 Days

Description

This course explores the features introduced in Java 21, the latest long-term support (LTS) release and a major step forward in the language’s evolution. Participants dive into virtual threads for scalable concurrent programming, pattern matching enhancements including record patterns and switch refinements, sequenced collections for predictable ordering, and string templates for safer text processing. The course emphasizes practical application of these features, helping learners write more performant, maintainable, and expressive Java code that takes full advantage of modern hardware.

Audience

This course is designed for experienced Java developers proficient with Java 17 who want to master the latest LTS innovations. It’s ideal for architects designing high-performance systems and teams modernizing applications to Java 21 who want to put virtual threads and advanced pattern matching to work.

Objectives

  • Implement virtual threads to build highly scalable concurrent applications
  • Apply record patterns and pattern matching for switch to destructure complex data
  • Use sequenced collections to maintain predictable element ordering
  • Leverage pattern matching enhancements for cleaner and safer code
  • Understand the virtual thread execution model and best practices
  • Work with unnamed patterns and variables for more concise code
  • Utilize the structured concurrency API for coordinated task management
  • Apply key encapsulation mechanism and generational ZGC improvements

Prerequisites

Participants must be proficient in Java 17, including records, sealed classes, pattern matching, switch expressions, and text blocks. Strong understanding of Java concurrency fundamentals (threads, locks, and synchronization) is essential. Professional experience with multi-threaded Java applications is highly recommended.

Course Outline

Module 1: Java 21 Overview

  • Java 21 as a Long-Term Support Release
  • Evolution from Java 17 to Java 21
  • Major Themes — Concurrency and Pattern Matching
  • Feature Status — Final, Preview, and Incubator

Module 2: Virtual Threads (Project Loom)

  • Motivation and Platform Threads Limitations
  • Virtual Threads Fundamentals
  • Creating and Running Virtual Threads
  • Virtual Thread Execution Model
  • Pinning and Carrier Threads
  • Performance Characteristics and Scalability
  • Best Practices and Anti-Patterns

Module 3: Virtual Threads in Practice

  • Refactoring Thread-per-Request Applications
  • Virtual Threads and Thread Pools
  • Integration with ExecutorService
  • Debugging and Monitoring Virtual Threads

Module 4: Structured Concurrency (Preview)

  • Introduction to Structured Concurrency
  • StructuredTaskScope API
  • Coordinating Concurrent Subtasks
  • Error Handling in Structured Concurrency
  • Shutdown Policies

Module 5: Record Patterns

  • Pattern Matching Recap
  • Record Patterns in instanceof
  • Nested Record Patterns
  • Destructuring Records
  • Record Patterns for Data Queries

Module 6: Pattern Matching for switch Enhancements

  • Refined Pattern Matching for switch
  • Record Patterns in switch
  • Guarded Patterns and when Clauses
  • Pattern Coverage and Exhaustiveness
  • Combining Patterns with Sealed Types

Module 7: Unnamed Patterns and Variables

  • Unnamed Variables with Underscore
  • Unnamed Patterns for Irrelevant Components
  • Applications in switch and Destructuring

Module 8: Sequenced Collections

  • Motivation for Sequenced Collections
  • SequencedCollection Interface
  • SequencedSet and SequencedMap
  • First and Last Element Access
  • Reversed Views

Module 9: String Templates (Preview)

  • String Interpolation Challenges
  • String Template Syntax
  • Template Processors
  • Custom Template Processors
  • Safety and Security Benefits

Module 10: Additional Enhancements

  • Generational ZGC
  • Key Encapsulation Mechanism API
  • Deprecations and Removals
  • Preparing for Future Java Releases