Get the FREE Ultimate OpenClaw Setup Guide →
I

Java

Verified

@ivangdavila

npx machina-cli add skill @ivangdavila/java --openclaw
Files (1)
SKILL.md
1.8 KB

Quick Reference

TopicFile
Nulls, Optional, autoboxingnulls.md
Collections and iteration trapscollections.md
Generics and type erasuregenerics.md
Concurrency and synchronizationconcurrency.md
Classes, inheritance, memoryclasses.md
Streams and CompletableFuturestreams.md
Testing (JUnit, Mockito)testing.md
JVM, GC, modulesjvm.md

Critical Rules

  • == compares references, not content — always use .equals() for strings
  • Override equals() must also override hashCode() — HashMap/HashSet break otherwise
  • Optional.get() throws if empty — use orElse(), orElseGet(), or ifPresent()
  • Modifying while iterating throws ConcurrentModificationException — use Iterator.remove()
  • Type erasure: generic type info gone at runtime — can't do new T() or instanceof List<String>
  • volatile ensures visibility, not atomicity — count++ still needs synchronization
  • Unboxing null throws NPE — Integer i = null; int x = i; crashes
  • Integer == Integer uses reference for values outside -128 to 127 — use .equals()
  • Try-with-resources auto-closes — implement AutoCloseable, Java 7+
  • Inner classes hold reference to outer — use static nested class if not needed
  • Streams are single-use — can't reuse after terminal operation
  • thenApply vs thenCompose — compose for chaining CompletableFutures
  • Records are implicitly final — can't extend, components are final
  • serialVersionUID mismatch breaks deserialization — always declare explicitly

Source

git clone https://clawhub.ai/ivangdavila/javaView on GitHub

Overview

Master robust Java practices to ship safer, more reliable code by avoiding null traps, incorrect equality, and concurrency bugs. This skill compiles practical rules, patterns, and examples drawn from core topics like null handling, collections, generics, streams, and testing.

How This Skill Works

The skill codifies concrete guidelines and anti-patterns, aligned with Java 7+ features (try-with-resources, Optional, streams). It teaches safe implementations for common pitfalls—such as using equals for content comparisons, avoiding Optional.get() on empty, and proper iteration with Iterator.remove when mutating collections.

When to Use It

  • Designing domain models where correct content equality matters (uses equals/hashCode).
  • Building concurrency-safe components that require correct visibility and synchronization.
  • Working with streams and CompletableFuture chains to avoid common pitfall patterns.
  • Implementing resource management with try-with-resources and AutoCloseable.
  • Designing generic APIs and avoiding runtime type checks due to type erasure.

Quick Start

  1. Step 1: Audit null handling in your data models and replace risky null checks with Optional or guards.
  2. Step 2: Implement equals() and hashCode() for a sample domain class and verify string comparisons use .equals().
  3. Step 3: Add a small concurrency-safe example (e.g., a counter or a synchronized block) and a safe iteration example using Iterator.remove().

Best Practices

  • Prefer Optional for nullable data and avoid Optional.get() on empty; use orElse/orElseGet/ifPresent.
  • Override equals() and hashCode() together to keep HashMap/HashSet consistent.
  • When mutating a collection during iteration, use Iterator.remove() to avoid ConcurrentModificationException.
  • Be careful with Integer and string comparisons: use .equals() for value comparison (not == for Integers); avoid relying on reference equality.
  • Understand that volatile provides visibility, not atomicity; use synchronized blocks or atomic types for compound actions.

Example Use Cases

  • A User class with properly implemented equals() and hashCode() and immutable fields.
  • A thread-safe counter implemented with AtomicInteger or synchronized methods.
  • A list-processing method that removes elements via Iterator.remove() without CME.
  • A CompletableFuture pipeline that uses thenCompose to chain asynchronous calls.
  • A resource-handling class used inside a try-with-resources block with AutoCloseable.

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers