java
✓Verified·Scanned 2/18/2026
Write robust Java avoiding null traps, equality bugs, and concurrency pitfalls.
from clawhub.ai·v317d959·4.7 KB·0 installs
Scanned from 1.0.0 at 317d959 · Transparency log ↗
$ vett add clawhub.ai/ivangdavila/java
String Gotchas
==compares references, not content — always use.equals()for strings- String pool: literals interned,
new String()not —"a" == "a"true,new String("a") == "a"false - Strings are immutable — concatenation in loop creates garbage, use
StringBuilder null.equals(x)throws NPE — use"literal".equals(variable)orObjects.equals()
Null Handling
- NPE is most common exception — check nulls or use
Optional<T> Optional.get()throws if empty — useorElse(),orElseGet(), orifPresent()- Don't use Optional for fields or parameters — intended for return types
@Nullableand@NonNullannotations help static analysis — not enforced at runtime- Primitive types can't be null — but wrappers (
Integer) can, autoboxing hides this
Equality Contract
- Override
equals()must also overridehashCode()— HashMap/HashSet break otherwise equals()must be symmetric, transitive, consistent —a.equals(b)impliesb.equals(a)- Use
getClass()check, notinstanceof— unless explicitly designed for inheritance hashCode()must return same value for equal objects — unequal objects can share hash- Arrays:
Arrays.equals()for content —array.equals(other)uses reference comparison
Generics Traps
- Type erasure: generic type info gone at runtime — can't do
new T()orinstanceof List<String> - Raw types bypass safety —
List listallows any type, loses compile-time checks List<Dog>is not subtype ofList<Animal>— use wildcards:List<? extends Animal><?>is not same as<Object>— wildcard allows any type, Object only allows Object- Generic arrays forbidden —
new T[10]fails, useArrayList<T>instead
Collections Pitfalls
- Modifying while iterating throws
ConcurrentModificationException— use Iterator.remove() or copy Arrays.asList()returns fixed-size list — can't add/remove, backed by arrayList.of(),Set.of()return immutable — throw on modification attemptsHashMapallows null key and values —HashtableandConcurrentHashMapdon't- Sort requires
ComparableorComparator— ClassCastException if missing
Autoboxing Dangers
Integer == Integeruses reference for values outside -128 to 127 — use.equals()- Unboxing null throws NPE —
Integer i = null; int x = i;crashes - Performance: boxing in tight loops creates garbage — use primitives
Integer.valueOf()caches small values —new Integer()never caches (deprecated)
Concurrency
volatileensures visibility, not atomicity —count++still needs synchronizationsynchronizedon method locksthis— static synchronized locks class object- Double-checked locking broken without volatile — use holder pattern or enum for singletons
ConcurrentHashMapsafe but not atomic for compound ops — usecomputeIfAbsent()- Thread pool: don't create threads manually — use
ExecutorService
Exception Handling
- Checked exceptions must be caught or declared — unchecked (RuntimeException) don't
- Try-with-resources auto-closes — implement
AutoCloseable, Java 7+ - Catch specific exceptions first — more general catch later or unreachable code error
- Don't catch
Throwable— includesErrorwhich shouldn't be caught finallyalways runs — even on return, but return in finally overrides try's return
Inheritance Quirks
privatemethods not overridden — new method with same name in childstaticmethods hide, don't override — called based on reference type, not objectsuper()must be first statement in constructor — no logic beforefinalmethods can't be overridden —finalclass can't be extended- Fields don't participate in polymorphism — accessed by reference type
Memory Management
- Leaked listeners/callbacks prevent GC — remove references when done
WeakReferencefor caches — allows GC when memory neededstaticcollections grow forever — clear or use weak/soft references- Inner classes hold reference to outer — use static nested class if not needed
finalize()deprecated — use Cleaner or try-with-resources
Modern Java
- Records (16+): immutable data carriers — auto-generates equals, hashCode, toString
- Sealed classes (17+): restrict inheritance —
permitsclause lists allowed subclasses - Pattern matching in switch (21+): type patterns and guards — cleaner than instanceof chains
- Virtual threads (21+): lightweight concurrency — don't pool, create freely
varfor local variables (10+) — inferred type, still strongly typed