I
Java
Verified@ivangdavila
npx machina-cli add skill @ivangdavila/java --openclawFiles (1)
SKILL.md
1.8 KB
Quick Reference
| Topic | File |
|---|---|
| Nulls, Optional, autoboxing | nulls.md |
| Collections and iteration traps | collections.md |
| Generics and type erasure | generics.md |
| Concurrency and synchronization | concurrency.md |
| Classes, inheritance, memory | classes.md |
| Streams and CompletableFuture | streams.md |
| Testing (JUnit, Mockito) | testing.md |
| JVM, GC, modules | jvm.md |
Critical Rules
==compares references, not content — always use.equals()for strings- Override
equals()must also overridehashCode()— HashMap/HashSet break otherwise Optional.get()throws if empty — useorElse(),orElseGet(), orifPresent()- Modifying while iterating throws
ConcurrentModificationException— use Iterator.remove() - Type erasure: generic type info gone at runtime — can't do
new T()orinstanceof List<String> volatileensures visibility, not atomicity —count++still needs synchronization- Unboxing null throws NPE —
Integer i = null; int x = i;crashes Integer == Integeruses 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
thenApplyvsthenCompose— compose for chaining CompletableFutures- Records are implicitly final — can't extend, components are final
serialVersionUIDmismatch breaks deserialization — always declare explicitly
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
- Step 1: Audit null handling in your data models and replace risky null checks with Optional or guards.
- Step 2: Implement equals() and hashCode() for a sample domain class and verify string comparisons use .equals().
- 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