> java-language
Modern Java standards (21+) including Records, Pattern Matching, and Virtual Threads. Use when working with Java 21+ Records, sealed classes, or pattern matching features. (triggers: **/*.java, pom.xml, build.gradle, record, switch, sealed, var, virtual thread, stream, optional)
curl "https://skillshub.wtf/HoangNguyen0403/agent-skills-standard/java-language?format=md"Java Language Patterns
Priority: P0 (CRITICAL)
Modern Java (21+) standards for concise, immutable, and expressive code.
Implementation Guidelines
- Records: Use
recordfor DTOs/Value Objects. Avoid Lombok@Datawhere possible. - Local Variables: Use
varfor clear, inferred types. Explicit types for interfaces. - Switch: Use Switch Expressions
->and Pattern Matching overif/elsechains. - Text Blocks: Use
"""for JSON/SQL strings. Avoid concatenation+. - Pattern Matching: Use
instanceofwith binding:if (obj instanceof String s). - Sealed Classes: Use
sealed interface/classfor restricted hierarchies (Domain Models). - Collections: Use
List.of(),Map.of(),Set.of()for unmodifiable collections. - Streams: Use
stream()for transformations. PrefertoList()(Java 16+) overcollect(Collectors.toList()). - Optional: Return
Optional<T>overnull. Usemap,filter,ifPresent. Never useOptionalfields/params. - Virtual Threads: Prefer
Executors.newVirtualThreadPerTaskExecutor()for high-throughput I/O.
Anti-Patterns
- No
null:**No Nulls**: Return Optional or empty collections; avoid null parameters. - No Raw Types:
**Use Generics**: Never use raw types like List. - No Old Switch:
**No Fall-through**: Use Switch Expressions ->. - Boilerplate:
**No manual get/set**: Use Records or value objects. - Blocking:
**No synchronized**: Use java.util.concurrent utilities or Virtual Threads.
Code
// Record + Pattern Matching
public record User(String id, String name) {}
public String handle(Object obj) {
return switch (obj) {
case User(var id, var name) -> "User: " + name; // Record Patterns (Java 21)
case String s when s.length() > 5 -> "Long String: " + s;
case null -> "It's null";
default -> "Unknown";
};
}
// Virtual Threads (Loom)
try (var scope = new StructuredTaskScope.ShutdownOnFailure()) {
var userDetails = scope.fork(() -> fetchUser());
var orders = scope.fork(() -> fetchOrders());
scope.join().throwIfFailed(); // Wait for both
}
Related Topics
best-practices | concurrency | testing
> related_skills --same-repo
> typescript-tooling
Development tools, linting, and build config for TypeScript. Use when configuring ESLint, Prettier, Jest, Vitest, tsconfig, or any TS build tooling. (triggers: tsconfig.json, .eslintrc.*, jest.config.*, package.json, eslint, prettier, jest, vitest, build, compile, lint)
> typescript-security
Secure coding practices for TypeScript. Use when validating input, handling auth tokens, sanitizing data, or managing secrets and sensitive configuration. (triggers: **/*.ts, **/*.tsx, validate, sanitize, xss, injection, auth, password, secret, token)
> typescript-language
Modern TypeScript standards for type safety and maintainability. Use when working with types, interfaces, generics, enums, unions, or tsconfig settings. (triggers: **/*.ts, **/*.tsx, tsconfig.json, type, interface, generic, enum, union, intersection, readonly, const, namespace)
> typescript-best-practices
Idiomatic TypeScript patterns for clean, maintainable code. Use when writing or refactoring TypeScript classes, functions, modules, or async logic. (triggers: **/*.ts, **/*.tsx, class, function, module, import, export, async, promise)