Java

From G1Joshi/Agent-Skills · View interactive version

Java is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert modern Java (Java 17/21 LTS) assistance covering records, pattern matching, virtual threads (Project Loom), Stream API, and JVM memory tuning. Use when writing idiomatic enterprise Java, optimizing garbage collection, or building concurrent backend applications.

How to use this skill

Skill file

Java

Modern Java development with Java 17+ features, streams, and enterprise patterns.

When to Use

Quick Start

// Modern record with validation
public record User(String id, String name, String email) {
    public User {
        Objects.requireNonNull(id, "id must not be null");
        Objects.requireNonNull(name, "name must not be null");
    }
}

Core Concepts

Virtual Threads (Project Loom - Java 21+ LTS)

Lightweight user-mode threads managed by the JVM rather than the OS, allowing synchronous blocking code to scale to millions of concurrent tasks:

import java.net.URI;
import java.net.http.*;
import java.util.concurrent.Executors;

public class VirtualThreadDemo {
    public static void main(String[] args) {
        // Runs each concurrent task on an ephemeral virtual thread
        try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
            for (int i = 0; i < 10_000; i++) {
                final int taskId = i;
                executor.submit(() -> {
                    // Blocking I/O unmounts virtual thread automatically
                    Thread.sleep(100);
                    return "Task " + taskId + " complete";
                });
            }
        } // Executor awaits completion of all virtual threads
    }
}

Records & Pattern Matching (Java 17/21)

Concise immutable data carriers with expressive pattern matching:

public sealed interface Transaction permits Payment, Refund {}
public record Payment(String transactionId, double amount) implements Transaction {}
public record Refund(String transactionId, double amount, String reason) implements Transaction {}

public class TransactionHandler {
    public static String audit(Transaction tx) {
        return switch (tx) {
            case Payment p when p.amount() > 10_000 -> "Large Payment Audit: " + p.transactionId();
            case Payment p                          -> "Standard Payment: " + p.transactionId();
            case Refund r                           -> "Refund: " + r.reason();
        };
    }
}

Modern Foreign Function & Memory API (Project Panama)

Interacts directly with native C libraries and off-heap memory safely without JNI:

import java.lang.foreign.*;

public class NativeInterop {
    public static void main(String[] args) {
        try (Arena arena = Arena.ofConfined()) {
            MemorySegment nativeString = arena.allocateFrom("Hello Native Memory!");
            System.out.println("Allocated byte size: " + nativeString.byteSize());
        } // Off-heap memory is automatically deallocated here
    }
}

Common Patterns

Immutable Collection Transformations with Streams

Problem: Verbose collection filtering and grouping introducing mutable intermediate states.

Solution:

// Immutable collections
var list = List.of("a", "b", "c");
var map = Map.of("key1", "value1", "key2", "value2");

// Stream operations
List<String> names = users.stream()
    .filter(User::active)
    .map(User::name)
    .sorted()
    .toList();

// Collectors grouping
Map<String, List<User>> byRole = users.stream()
    .collect(Collectors.groupingBy(User::role));

Optional

Problem: Null reference returns causing unpredictable NullPointerExceptions without compile-time safeguards.

Solution:

Optional<User> user = Optional.ofNullable(findUser(id));

String name = user
    .map(User::name)
    .orElse("Unknown");

user.ifPresentOrElse(
    u -> process(u),
    () -> handleMissing()
);

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| --------------------------------- | -------------------------- | -------------------------------- |

| NullPointerException | Null value access | Use Optional or null checks |

| ClassCastException | Invalid type cast | Use instanceof pattern matching |

| ConcurrentModificationException | Modifying during iteration | Use Iterator.remove() or streams |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.