home / skills / gentleman-programming / gentleman-skills / java-21

java-21 skill

/community/java-21

This skill helps you write modern safe Java 21 code using records, sealed types, and virtual threads with best-practice guidance.

npx playbooks add skill gentleman-programming/gentleman-skills --skill java-21

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
2.7 KB
---
name: java-21
description: >
  Java 21 language and runtime patterns for modern, safe code.
  Trigger: When writing Java 21 code using records, sealed types, or virtual threads.
metadata:
  author: diegnghrmr
  version: "1.0"
---

## When to Use

Load this skill when:
- Writing Java 21 application or library code
- Designing immutable DTOs or value objects
- Modeling closed hierarchies with sealed types
- Using virtual threads for blocking I/O

## Critical Patterns

### Pattern 1: Records for immutable data

Use records for DTOs and value objects, validate in compact constructors.

### Pattern 2: Sealed types + pattern matching

Use sealed interfaces/classes and switch pattern matching for exhaustiveness.

### Pattern 3: Virtual threads for I/O

Use virtual threads to handle blocking I/O without large thread pools.

## Code Examples

### Example 1: Record with validation

```java
package com.acme.user;

public record Email(String value) {
  public Email {
    if (value == null || !value.contains("@")) {
      throw new IllegalArgumentException("Invalid email");
    }
  }
}
```

### Example 2: Sealed hierarchy + switch pattern matching

```java
package com.acme.payment;

public sealed interface Payment permits Card, BankTransfer { }

public record Card(String last4) implements Payment { }
public record BankTransfer(String iban) implements Payment { }

public final class PaymentPrinter {
  public String describe(Payment payment) {
    return switch (payment) {
      case Card card -> "card-" + card.last4();
      case BankTransfer bank -> "iban-" + bank.iban();
    };
  }
}
```

### Example 3: Virtual threads for blocking calls

```java
package com.acme.io;

import java.util.concurrent.Executors;

public final class Fetcher {
  public void fetchAll(java.util.List<String> urls) throws Exception {
    try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
      for (String url : urls) {
        executor.submit(() -> blockingFetch(url));
      }
    }
  }

  private void blockingFetch(String url) {
    // perform blocking I/O here
  }
}
```

## Anti-Patterns

### Don't: Use mutable data carriers

```java
// BAD: mutable DTO
public class UserDto {
  public String name;
  public String email;
}
```

### Don't: Spin up raw platform threads per request

```java
// BAD: expensive and unbounded
new Thread(() -> blockingFetch("https://api" )).start();
```

## Quick Reference

| Task | Pattern |
|------|---------|
| Immutable DTOs | Use records with validation |
| Closed hierarchies | Use sealed interfaces + switch |
| Blocking I/O scale | Use virtual threads executor |

## Resources

- Java 21 Release Notes: https://www.oracle.com/java/technologies/javase/21-relnote-issues.html
- JEP Index: https://openjdk.org/jeps/0

Overview

This skill packages Java 21 language and runtime patterns for modern, safe code. It focuses on records for immutable data, sealed types with pattern matching, and virtual threads for scalable blocking I/O. Use it to adopt concise, expressive, and correct idioms when targeting Java 21.

How this skill works

The skill highlights concrete patterns and small code idioms you can apply directly in application and library code. It shows how to validate data in record compact constructors, model closed hierarchies with sealed types and exhaustive switch pattern matching, and replace heavy platform threads with virtual-thread executors for blocking tasks. Each pattern includes the intent, benefits, and anti-pattern guidance so you can apply them safely.

When to use it

  • Writing application or library code targeting Java 21
  • Designing immutable DTOs or value objects that require validation
  • Modeling closed or evolving type hierarchies where exhaustiveness matters
  • Handling large numbers of blocking I/O tasks without a heavyweight thread pool
  • Upgrading legacy code to safer, more concise language features

Best practices

  • Prefer records for DTOs and value objects; validate inputs in the compact constructor
  • Use sealed interfaces/classes to capture closed hierarchies and enable exhaustive switch patterns
  • Use switch pattern matching to centralize type-specific logic with compiler exhaustiveness checks
  • Use Executors.newVirtualThreadPerTaskExecutor() for blocking I/O to avoid creating raw platform threads
  • Avoid public mutable fields or mutable DTO types; prefer immutable records or well-encapsulated classes

Example use cases

  • Define Email, Money, or Identifier types as records with validation in the compact constructor
  • Model payment methods with a sealed Payment interface and implement Card/BankTransfer as records
  • Write a PaymentPrinter that uses switch pattern matching to produce stable, exhaustive descriptions
  • Fetch many URLs concurrently by using a virtual-thread-per-task executor for each blocking request
  • Refactor mutable service DTOs into records to reduce accidental state changes and simplify equals/hashCode

FAQ

Are records always appropriate for all DTOs?

Records are ideal for shallow, immutable data carriers. If you need mutable state, complex lifecycle, or inheritance beyond records, use a class instead.

When should I prefer virtual threads over platform threads?

Use virtual threads whenever the work is I/O-bound and blocking, and you expect many concurrent tasks. Platform threads still make sense for code that requires specific thread-local behavior or native thread affinity.