Home / Market / Effective Java

Effective Java

Review, refactor, and explain Java code using distilled Effective Java principles for API design, immutability, generics, enums, streams, exceptions, concurr...

skill★ 00 dlclawhubMIT-0Install in CosmosUpstream Source

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

  1. Prefer static factories when naming, caching, subtyping, or instance control makes construction clearer.
  2. Use builders when constructors would require many optional or interdependent parameters.
  3. Enforce singleton properties carefully; enum singletons are often simplest.
  4. Prevent instantiation of utility classes with a private constructor.
  5. Prefer dependency injection to hard-coded resources.
  6. Avoid unnecessary objects, especially in hot paths.
  7. Remove obsolete references that prevent garbage collection.
  8. Avoid finalizers and cleaners for normal cleanup.
  9. Use try-with-resources for deterministic resource release.

Methods Common to All Objects

  1. Override equals only when logical equality is needed and can satisfy the full contract.
  2. Always override hashCode when overriding equals.
  3. Implement toString for useful diagnostics and logging.
  4. Prefer copying alternatives to clone; if cloning, respect its tricky contract.
  5. Implement Comparable only with a consistent, documented total ordering.

Classes and Interfaces

  1. Minimize accessibility of every type and member.
  2. Use accessors instead of public mutable fields.
  3. Minimize mutability; immutable objects are simpler and safer.
  4. Favor composition and forwarding over inheritance for reuse.
  5. Design and document inheritance deliberately, or prohibit it.
  6. Prefer interfaces to abstract classes for type definitions.
  7. Design interfaces carefully because default methods can create compatibility traps.
  8. Do not use interfaces only to export constants.
  9. Prefer class hierarchies to tagged classes with mode fields.
  10. Prefer static member classes over nonstatic ones when no enclosing instance is needed.
  11. Keep one top-level class per source file.

Generics

  1. Do not use raw types in new code.
  2. Eliminate unchecked warnings; suppress only tightly and with justification.
  3. Prefer lists to arrays when generics are involved.
  4. Favor generic types over casts at use sites.
  5. Favor generic methods for reusable type-safe operations.
  6. Use bounded wildcards to make APIs flexible.
  7. Combine generics and varargs carefully; avoid heap pollution.
  8. Use type tokens for type-safe heterogeneous containers.

Enums and Annotations

  1. Use enums instead of integer constants for fixed sets.
  2. Do not rely on enum ordinals for data or persistence.
  3. Use EnumSet instead of bit fields.
  4. Use EnumMap instead of ordinal indexing.
  5. Model extensible enum-like behavior with interfaces when needed.
  6. Prefer annotations to naming patterns.
  7. Consistently use @Override.
  8. Use marker interfaces when type relationships matter at compile time.

Lambdas and Streams

  1. Prefer lambdas to anonymous classes for small function objects.
  2. Prefer method references when they are clearer than lambdas.
  3. Use standard functional interfaces before inventing new ones.
  4. Use streams judiciously; clarity beats novelty.
  5. Keep stream functions side-effect-free.
  6. Return collections or arrays rather than streams when callers need normal collection behavior.
  7. Use parallel streams only with evidence that they are correct and faster.

Methods

  1. Check parameters for validity and fail early.
  2. Make defensive copies when accepting or returning mutable objects across trust boundaries.
  3. Design method signatures carefully: names, parameters, overloads, and return types are API.
  4. Use overloading carefully; avoid surprising compile-time dispatch.
  5. Use varargs judiciously and avoid ambiguous or unsafe calls.
  6. Return empty collections or arrays, not null.
  7. Return Optional judiciously for absent results.
  8. Write documentation comments for exposed APIs.

General Programming

  1. Minimize the scope of local variables.
  2. Prefer enhanced for loops when indexes or iterators are not needed.
  3. Know and use the standard libraries.
  4. Use BigDecimal, int, or long for exact monetary calculations; avoid float or double for exact answers.
  5. Prefer primitives to boxed primitives unless nullability or generics require boxing.
  6. Avoid strings for data that has a better type.
  7. Beware repeated string concatenation in performance-sensitive loops.
  8. Refer to objects by interfaces when suitable.
  9. Prefer interfaces to reflection for normal application logic.
  10. Use native methods only when justified.
  11. Optimize only after measurement identifies a real problem.
  12. Follow generally accepted naming conventions.

Exceptions

  1. Use exceptions only for exceptional conditions.
  2. Use checked exceptions for recoverable conditions and runtime exceptions for programming errors.
  3. Avoid unnecessary checked exceptions that make APIs painful.
  4. Prefer standard exceptions when they fit.
  5. Translate lower-level exceptions while preserving the cause.
  6. Document every exception thrown by exposed APIs.
  7. Include failure-capture information in detail messages.
  8. Strive for failure atomicity.
  9. Do not ignore exceptions.

Concurrency

  1. Synchronize access to shared mutable data.
  2. Avoid excessive synchronization and avoid alien method calls inside locks.
  3. Prefer executors, tasks, and streams to raw threads.
  4. Prefer concurrency utilities to wait and notify.
  5. Document thread-safety guarantees.
  6. Use lazy initialization only when it is necessary and safely implemented.
  7. Do not depend on the thread scheduler for correctness.

Serialization

  1. Prefer alternatives to Java serialization.
  2. Implement Serializable very cautiously because it creates long-lived API and security obligations.
  3. Consider a custom serialized form when the default form exposes internals or wastes space.
  4. Write readObject defensively.
  5. Prefer enum types for instance control when compatible with the design.
  6. Consider serialization proxies for classes with invariants.

Fast Mapping by Task