Effective Java
Review, refactor, and explain Java code using distilled Effective Java principles for API design, immutability, generics, enums, streams, exceptions, concurr...
Documentation & Preview
Effective Java Item Map
This is a compact, paraphrased map of Effective Java, 3rd edition. Use it for item-level navigation and learning summaries; do not treat it as a substitute for the book.
Object Creation and Destruction
- Prefer static factories when naming, caching, subtyping, or instance control makes construction clearer.
- Use builders when constructors would require many optional or interdependent parameters.
- Enforce singleton properties carefully; enum singletons are often simplest.
- Prevent instantiation of utility classes with a private constructor.
- Prefer dependency injection to hard-coded resources.
- Avoid unnecessary objects, especially in hot paths.
- Remove obsolete references that prevent garbage collection.
- Avoid finalizers and cleaners for normal cleanup.
- Use try-with-resources for deterministic resource release.
Methods Common to All Objects
- Override
equalsonly when logical equality is needed and can satisfy the full contract. - Always override
hashCodewhen overridingequals. - Implement
toStringfor useful diagnostics and logging. - Prefer copying alternatives to
clone; if cloning, respect its tricky contract. - Implement
Comparableonly with a consistent, documented total ordering.
Classes and Interfaces
- Minimize accessibility of every type and member.
- Use accessors instead of public mutable fields.
- Minimize mutability; immutable objects are simpler and safer.
- Favor composition and forwarding over inheritance for reuse.
- Design and document inheritance deliberately, or prohibit it.
- Prefer interfaces to abstract classes for type definitions.
- Design interfaces carefully because default methods can create compatibility traps.
- Do not use interfaces only to export constants.
- Prefer class hierarchies to tagged classes with mode fields.
- Prefer static member classes over nonstatic ones when no enclosing instance is needed.
- Keep one top-level class per source file.
Generics
- Do not use raw types in new code.
- Eliminate unchecked warnings; suppress only tightly and with justification.
- Prefer lists to arrays when generics are involved.
- Favor generic types over casts at use sites.
- Favor generic methods for reusable type-safe operations.
- Use bounded wildcards to make APIs flexible.
- Combine generics and varargs carefully; avoid heap pollution.
- Use type tokens for type-safe heterogeneous containers.
Enums and Annotations
- Use enums instead of integer constants for fixed sets.
- Do not rely on enum ordinals for data or persistence.
- Use
EnumSetinstead of bit fields. - Use
EnumMapinstead of ordinal indexing. - Model extensible enum-like behavior with interfaces when needed.
- Prefer annotations to naming patterns.
- Consistently use
@Override. - Use marker interfaces when type relationships matter at compile time.
Lambdas and Streams
- Prefer lambdas to anonymous classes for small function objects.
- Prefer method references when they are clearer than lambdas.
- Use standard functional interfaces before inventing new ones.
- Use streams judiciously; clarity beats novelty.
- Keep stream functions side-effect-free.
- Return collections or arrays rather than streams when callers need normal collection behavior.
- Use parallel streams only with evidence that they are correct and faster.
Methods
- Check parameters for validity and fail early.
- Make defensive copies when accepting or returning mutable objects across trust boundaries.
- Design method signatures carefully: names, parameters, overloads, and return types are API.
- Use overloading carefully; avoid surprising compile-time dispatch.
- Use varargs judiciously and avoid ambiguous or unsafe calls.
- Return empty collections or arrays, not
null. - Return
Optionaljudiciously for absent results. - Write documentation comments for exposed APIs.
General Programming
- Minimize the scope of local variables.
- Prefer enhanced
forloops when indexes or iterators are not needed. - Know and use the standard libraries.
- Use
BigDecimal,int, orlongfor exact monetary calculations; avoid float or double for exact answers. - Prefer primitives to boxed primitives unless nullability or generics require boxing.
- Avoid strings for data that has a better type.
- Beware repeated string concatenation in performance-sensitive loops.
- Refer to objects by interfaces when suitable.
- Prefer interfaces to reflection for normal application logic.
- Use native methods only when justified.
- Optimize only after measurement identifies a real problem.
- Follow generally accepted naming conventions.
Exceptions
- Use exceptions only for exceptional conditions.
- Use checked exceptions for recoverable conditions and runtime exceptions for programming errors.
- Avoid unnecessary checked exceptions that make APIs painful.
- Prefer standard exceptions when they fit.
- Translate lower-level exceptions while preserving the cause.
- Document every exception thrown by exposed APIs.
- Include failure-capture information in detail messages.
- Strive for failure atomicity.
- Do not ignore exceptions.
Concurrency
- Synchronize access to shared mutable data.
- Avoid excessive synchronization and avoid alien method calls inside locks.
- Prefer executors, tasks, and streams to raw threads.
- Prefer concurrency utilities to
waitandnotify. - Document thread-safety guarantees.
- Use lazy initialization only when it is necessary and safely implemented.
- Do not depend on the thread scheduler for correctness.
Serialization
- Prefer alternatives to Java serialization.
- Implement
Serializablevery cautiously because it creates long-lived API and security obligations. - Consider a custom serialized form when the default form exposes internals or wastes space.
- Write
readObjectdefensively. - Prefer enum types for instance control when compatible with the design.
- Consider serialization proxies for classes with invariants.
Fast Mapping by Task
- New value object: items 2, 10-14, 17, 49-50, 56.
- Public API review: items 1-2, 15-22, 31, 49-56, 70-75, 82.
- Collection-heavy utility: items 26-33, 45-48, 54-55, 57-59.
- Concurrent service: items 5, 17, 78-84.
- Enum modeling: items 34-38.
- Legacy serialization: items 85-90.
- Performance pass: items 6, 45-48, 59-61, 63, 67, 78-81.