Java Language
ASecurityUse Java 21 pattern matching for `switch` instead of an `instanceof` chain: ```java static void handle(Object value) { switch (value) { case TypeA a -> a.methodA(); case TypeB b -> b.methodB(); case TypeC c -> c.methodC(); case TypeD d -> d.methodD(); default -> throw new IllegalArgumentException( "Unsupported type: " + value.getClass().getName()); } } ``` The pattern variable (`a`, `b`, etc.) is automatically scoped to its matching case, so no explicit cast is needed. If the values belong to...
- 584 stars
- 0 votes
- 0 copies
- 1 view
- Added September 5, 2026
Security analysis
100/100Pro scans all 18 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill java-language --agent claude-codeAre you the author of Java Language?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-java-language-6c0d067e)Use Java 21 pattern matching for `switch` instead of an `instanceof` chain:
```java
static void handle(Object value) {
switch (value) {
case TypeA a -> a.methodA();
case TypeB b -> b.methodB();
case TypeC c -> c.methodC();
case TypeD d -> d.methodD();
default -> throw new IllegalArgumentException(
"Unsupported type: " + value.getClass().getName());
}
}
```
The pattern variable (`a`, `b`, etc.) is automatically scoped to its matching case, so no explicit cast is needed. If the values belong to a closed domain, model them as a `sealed` interface with the four permitted implementations; then the compiler can verify exhaustiveness and you can omit `default`:
```java
sealed interface Command permits Create, Update, Delete, Read {}
static Result handle(Command command) {
return switch (command) {
case Create c -> create(c);
case Update u -> update(u);
case Delete d -> delete(d);
case Read r -> read(r);
};
}
```
Use an expression-returning `switch` when each branch produces a value, and keep a simple pattern-matching `switch` statement when the branches only perform actions. For nullable input, handle `case null` explicitly or reject null at the boundary.
Files in this skill
- eval-1.baseline.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- trigger-1.md
- trigger-10.md
- trigger-11.md
- trigger-2.md
- trigger-3.md
- trigger-4.md
- trigger-5.md
- trigger-6.md
- trigger-7.md
- trigger-8.md
- trigger-9.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.