Skills DirectorySkills Directory
SkillsLearnSecurityCategoriesDocsBlogPro
Sign InSubmit Skill
Skills Directory

Security-tested agent skills for Claude, coding agents, and AI workflows.

Directory

  • Browse Skills
  • All Skills A–Z
  • Claude Skills
  • Claude Code Skills
  • Agent Skills
  • Categories
  • Authors
  • Submit a Skill

Learn

  • Learn Hub
  • Install Claude Skills
  • Write SKILL.md
  • Skills vs MCP
  • Directories Compared

Security

  • Security
  • Methodology
  • Secure Claude Skills
  • Security Badges
  • Chrome Extension
  • Skill Manager

Company

  • About
  • Community
  • Blog
  • API Docs
  • Advertise

2026 Skills Directory. All rights reserved.

ProTermsPrivacyRefunds
Back to skills

Java

ASecurity

Language-specific super-code guidelines for java.

7 stars
0 votes
0 copies
0 views
Added 9/22/2026
documentationgojavasqlexpress

Security Analysis

A100/100

Scanned 9/22/2026

$npx -y skills add JantonioFC/skillsbank --skill java --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Java?

Add the live security badge to your README — it updates automatically with every re-scan.

Security grade badge for Java
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/jantoniofc-java-skillsbank/badge)](https://www.skillsdirectory.com/skills/jantoniofc-java-skillsbank)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
Files
SKILL.md
---
name: java
description: "Language-specific super-code guidelines for java."
risk: safe
source: community
date_added: "2026-06-16"
---
# Java: Idiomatic Efficiency Reference

## Table of Contents
1. [Streams & Collections](#streams)
2. [Optional](#optional)
3. [Records & Data Classes](#records)
4. [Switch Expressions](#switch)
5. [Concurrency](#concurrency)
6. [Error Handling](#errors)
7. [Anti-patterns specific to Java](#antipatterns)

---

## 1. Streams & Collections {#streams}

```java
// ❌ Imperative accumulation
List<String> result = new ArrayList<>();
for (Item item : items) {
    if (item.isActive()) result.add(item.getName().toUpperCase());
}

// ✅
List<String> result = items.stream()
    .filter(Item::isActive)
    .map(item -> item.getName().toUpperCase())
    .toList(); // Java 16+; use .collect(Collectors.toList()) before
```

```java
// ❌ Manual grouping
Map<String, List<Item>> grouped = new HashMap<>();
for (Item item : items) {
    grouped.computeIfAbsent(item.getCategory(), k -> new ArrayList<>()).add(item);
}

// ✅
Map<String, List<Item>> grouped = items.stream()
    .collect(Collectors.groupingBy(Item::getCategory));
```

```java
// ❌ Manual sum
int total = 0;
for (Order o : orders) total += o.getAmount();

// ✅
int total = orders.stream().mapToInt(Order::getAmount).sum();
```

**Prefer method references (`Item::isActive`) over equivalent lambdas (`item -> item.isActive()`).**

---

## 2. Optional {#optional}

```java
// ❌ Null check chain
String city = null;
if (user != null && user.getAddress() != null) {
    city = user.getAddress().getCity();
}

// ✅
String city = Optional.ofNullable(user)
    .map(User::getAddress)
    .map(Address::getCity)
    .orElse(null);
```

```java
// ❌ Optional.get() without isPresent()
String name = optional.get(); // throws if empty

// ✅
String name = optional.orElse("default");
// or: optional.orElseThrow(() -> new IllegalStateException("name required"));
```

```java
// ❌ Optional as a field or parameter (anti-pattern)
class User { private Optional<String> nickname; }

// ✅ — Optional is for return types only
class User { private String nickname; } // nullable field
public Optional<String> getNickname() { return Optional.ofNullable(nickname); }
```

---

## 3. Records & Data Classes {#records}

```java
// ❌ Manual POJO
class Point {
    private final int x, y;
    public Point(int x, int y) { this.x = x; this.y = y; }
    public int getX() { return x; }
    public int getY() { return y; }
    // + equals, hashCode, toString...
}

// ✅ (Java 16+)
record Point(int x, int y) {}
```

```java
// ❌ Builder pattern for a 2-field object
User user = new User.Builder().name("Alice").age(30).build();

// ✅ — use record or constructor directly for small objects
record User(String name, int age) {}
var user = new User("Alice", 30);
```

**Use `record` for any immutable data carrier. Keep builders only for objects with many optional fields.**

---

## 4. Switch Expressions {#switch}

```java
// ❌ Switch statement with fall-through and break
String label;
switch (status) {
    case ACTIVE: label = "Active"; break;
    case INACTIVE: label = "Inactive"; break;
    default: label = "Unknown";
}

// ✅ (Java 14+)
String label = switch (status) {
    case ACTIVE -> "Active";
    case INACTIVE -> "Inactive";
    default -> "Unknown";
};
```

```java
// ❌ instanceof + cast
if (shape instanceof Circle) {
    Circle c = (Circle) shape;
    return c.radius() * c.radius() * Math.PI;
}

// ✅ Pattern matching (Java 16+)
if (shape instanceof Circle c) {
    return c.radius() * c.radius() * Math.PI;
}
```

---

## 5. Concurrency {#concurrency}

```java
// ❌ Raw Thread creation
Thread t = new Thread(() -> doWork());
t.start();

// ✅
ExecutorService exec = Executors.newVirtualThreadPerTaskExecutor(); // Java 21
exec.submit(() -> doWork());
```

```java
// ❌ synchronized on this for fine-grained state
synchronized(this) { counter++; }

// ✅
AtomicInteger counter = new AtomicInteger();
counter.incrementAndGet();
```

**Prefer `CompletableFuture.allOf()` over blocking `.get()` chains for parallel async work.**

---

## 6. Error Handling {#errors}

```java
// ❌ Catching Exception to log and swallow
try {
    risky();
} catch (Exception e) {
    log.error("error", e);
}

// ✅ — rethrow as unchecked if you can't handle it
try {
    risky();
} catch (IOException e) {
    throw new UncheckedIOException(e);
}
```

```java
// ❌ Checked exceptions declared on every method
public void process() throws IOException, SQLException, ParseException { ... }

// ✅ — wrap at the boundary; internal methods throw unchecked
```

---

## 7. Anti-patterns specific to Java {#antipatterns}

| Anti-pattern | Preferred |
|---|---|
| `new ArrayList<String>()` (Java 7+) | `new ArrayList<>()` (diamond) |
| `"string".equals(variable)` (Yoda) | `Objects.equals(variable, "string")` |
| `for (int i = 0; i < list.size(); i++)` | enhanced for or stream |
| `StringBuffer` in single-threaded code | `StringBuilder` |
| `e.printStackTrace()` | `log.error("msg", e)` |
| `null` return for "not found" | `Optional<T>` return type |
| Public fields | private + accessor, or `record` |
| Mutable `static` fields | avoid; use dependency injection |
| `instanceof` + cast without pattern matching | pattern matching (Java 16+) |


## Limitations
- These are language-specific guidelines and do not cover overall architectural decisions.
- Over-compression might reduce readability; apply judgement.

Attribution

JantonioFCJantonioFC
View sourceSee grades on GitHubMore from JantonioFC →
SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments (0)

No comments yet. Be the first to comment!

SSkills DirectorySkills Directory

Ship a skill? Prove it's safe.

Free 120-pattern security scan, letter grade, and an embeddable README badge.

Submit a skill

Related Skills

Context Fundamentals

Understand the components, mechanics, and constraints of context in agent systems. Use when designing agent architectures, debugging context-related failures, or optimizing context usage.

179001 votes

Architecture Diagram Creator

Create comprehensive HTML architecture diagrams with data flows, business context, and system architecture.

6661 votes

release-notes

Draft release notes and changelog entries from git history or merged PRs between two refs (tags/SHAs/branches), including breaking changes, migrations, and upgrade steps. Use when the user asks for release notes, changelog updates, or a GitHub Release draft.

1301 votes

docs-style-guide

Documentation style guide enforcer by @planetabhi. Applies and reviews the writing style guide when authoring or editing product documentation and tutorials. Use to check prose for voice, tense, word choice, inclusive language, formatting, code block, UI, Markdown, and number/date conventions.

11 votes

Docx

Use this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', '.dotx', or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx or .dotx files, inserting or replacing images in documents, performing find-and-replace in W...

1798860 votes
View all in documentation →