Java 21 Features -
Workshop
BY GANESH GIRI
Introduction
Welcome to the Java 21 Features Workshop. In
this workshop, we will explore the new
features introduced in Java 21 with practical
examples.
JEP Overview
Java Enhancement Proposal (JEP 1: JDK Enhancement-Proposal &
Roadmap Process)
◦ Proposing , discussing and implementing changes to the JDK
◦ Introducing New features , API language & performance improvement
◦ Document decision
◦ Detailed record why , how and what is being changed
◦ Process Drafting , submission and review , sponsorship , candidate status ,targeted , integrated
and Released
Development Machin Setup
Install SDKMAN (if not install)
◦ curl -s "https://get.sdkman.io" | bash
◦ source "$HOME/.sdkman/bin/sdkman-init.sh“
◦ Install java 21
◦ Sdk install java 21
Key Features (JDK 21)
Language Enhancements
◦ JEP 430: String Templates (Preview)
◦ JEP 440: Record Patterns
◦ JEP 441: Pattern Matching for switch
◦ JEP 443: Unnamed Patterns and Variables (Preview)
JVM & Performance
◦ JEP 439: Generational ZGC
◦ JEP 444: Virtual Threads
◦ JEP 445: Unnamed Classes (Preview)
Security & Tooling
◦ JEP 451: Prepare to Disallow Dynamic Agent Loading
Preview Vs Final
Preview Features
◦ Purpose: Allow developers to try out new features before they become permanent.
◦ Characteristics:
◦ Not enabled by default; require compiler flags (e.g., --enable-preview).
◦ May change or be removed in future versions.
◦ Feedback from the community is encouraged.
Final Features
◦ Purpose: Fully supported and stable features that are part of the Java SE standard.
◦ Characteristics:
◦ Enabled by default.
◦ Considered stable and safe for production use.
◦ Backward compatibility is maintained.
Unnamed Classes and Instance Main
Methods
Motivation
◦ Making Java more approachable, especially for beginners, educators, and
scripting use cases.
Before 21:
After 21:
Pattern Matching
for switch
Motivation:
Pattern Matching for switch enhances the switch statement and
expression so that it can be used for pattern matching. Reduce
verbosity and complexity.handle auto type safety.
Sealed Classes And Interface
Motivation
A class hierarchy enables us to reuse code via inheritance. However, the
class hierarchy can also serve other purposes. Code reuse is great, but
isn’t always our primary goal
Sealed Classes restrict which other classes or interfaces may extend or implement
them. This provides more control over the class hierarchy.
Example:
public abstract sealed class Shape permits Circle, Square {}
public final class Circle extends Shape {}
public final class Square extends Shape {}
Sealed Interface
Motivation
Provide more control over type hierarchies, improving both design
clarity and exhaustiveness checking in pattern matching.
To seal an interface, we can apply the sealed modifier to its declaration.
The permits clause then specifies the classes that are permitted to
implement the sealed interface:
Example:
public sealed interface Shape permits Circle, Rectangle, Square {}
Unnamed Patterns and
Variables (Record Patterns)
Motivation:
Java makes you acknowledge every variable being used ,when there is
no use of them.
Write more concise code , code readability improve
Record Patterns allow you to deconstruct record values in a concise and
readable way.
String Templates
Motivation:
Using the existing StringBuilder String.formate() or messageFormate verbose way to
concatenate variables
String Templates simplify the creation of strings that include values computed at
runtime. This feature enhances readability and maintainability of code.
Example:
String name = "World";
String greeting = STR."Hello, {name}!";
System.out.println(greeting);
Java Sequence Collections
Motivation:
Sequence collections in Java, such as List, provide a way to store
ordered collections of elements. They allow for easy access, insertion,
and removal of elements at specific positions. This is particularly useful
for scenarios where the order of elements matters, such as processing
sequences of data.
Collection Framework With
Sequenced Interface
Sequence Collection
Generational ZGC
Motivation:
1. Optimize for Object Lifetimes: Separates objects into young and old
generations to optimize collection strategies.
2. Improve Throughput: Reduces overhead by focusing frequent
collections on the young generation.
3. Maintain Low Latency: Ensures sub-millisecond pause times while
improving efficiency.
4. Scalability: Ideal for large heaps and multi-threaded environments.
Platform / Virtual Thread
Platform Thread
Virtual Threads
Motivation
Traditional Java threads are OS-level threads, which are heavyweight and limited in
number.
Virtual threads are lightweight, allowing millions of concurrent threads in a single
JVM instance.
Virtual Threads provide a new concurrency model that allows you to create a large
number of threads with minimal overhead. This feature improves the scalability of
concurrent applications.
Example:
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
executor.submit(() -> System.out.println("Hello from virtual thread"));
}
Vector API
Foreign Function & Memory
API
The Foreign Function & Memory API allows Java programs to
interoperate with code and data outside of the Java runtime. This
feature provides a safer and more efficient way to access native code.
Key Encapsulation Mechanism
(KEM) API
Why KEM?
In traditional public-key encryption, you encrypt the actual message
with the recipient’s public key. But this can be inefficient for large data.
Instead, KEM allows you to:
Generate a random symmetric key (used for actual data encryption).
Encapsulate this key using the recipient’s public key.
Send the encapsulated key to the recipient.
The recipient decapsulates it using their private key to retrieve the
symmetric key.
Overview of KEM
What next Java 24 Feature
Language Enhancements
JEP 488: Primitive Types in Patterns, instanceof, and switch (2nd
Preview)
JEP 492: Flexible Constructor Bodies (3rd Preview)
JEP 494: Module Import Declarations (2nd Preview)
JEP 495: Simple Source Files and Instance Main Methods (4th Preview)
Concurrency & Virtual Threads
JEP 487: Scoped Values (4th Preview)
JEP 491: Synchronize Virtual Threads without Pinning
JEP 499: Structured Concurrency (4th Preview)
Security & Cryptography
JEP 478: Key Derivation Function API (Preview)
JEP 496: Quantum-Resistant Lattice-Based Key Encapsulation
Mechanism
JEP 497: Quantum-Resistant Lattice-Based Digital Signature Algorithm
JEP 486: Permanently Disable the Security Manager
Core Libraries & APIs
JEP 485: Stream Gatherers
JEP 484: Class-File API
Thank you for attending!