Effective Java(R)

| Tags Reading  CS 

#Effective Java

ISBN: 0321356683 READ: 2014-08-17 RATING: 9/10

I spent two weeks on this book, nice read:), it gives you the best practices, every chapter focus on different best practice about java, if you have known the basic of java, you should definitely read this book. There are a lot of advises on how to make your code more correct and perfect.

The book is structured in 11 Chapters containing 78 items.There items covers these themes:

  • Creating and Destroying Objects
  • Methods Common to All Objects
  • Classes and Interfaces
  • Generics
  • Enum and Annotations
  • Methods
  • General Programming
  • Exceptions
  • Concurrency
  • Serialization

#Creating and Destroying Objects

###Item 1: Consider static factory methods instead of constructors.

Advantages:

  • Can have descriptive names, unlike constructors.
  • Not required to create new object, each time they are called.
  • Can return any subtype of their return type.
  • Reduce the verbosity of creating parameterized types. (Obviated by diamond in Java 7).

Disadvantages:

  • Classes with only static factory methods without non-private constructors cannot be sub-classed.
  • Not readily distinguishable from other static methods. Follow conventional names like valueOf, of, getInstance, newInstance, getType, and newType.

###Item 2: Consider a builder when faced with many constructor parameters.

  • Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters

  • Builder is set as a static inner class. Builder makes the object initialized immutable.

###Item 3: Enforce the singleton property with a private constructor or an enum type.

Three ways to implement Singleton pattern:

  • Public field
  • Static factory method
  • Enum Singleton

###Item 4: Enforce non-instantiability with a private constructor

Some utility classes like java.lang.Math or java.util.Arrays are not designed to be instantiated. For preventing them from instantiation and subclassing, make the constructor to be private.

###Item 5: Avoid creating unnecessary objects.

  • Prefer primitives to boxed primitives, and watch out for unintentional autoboxing.
  • An object can always be reused if it is immutable.
  • Ex: Do not do this BAD String s = new String(“string”); Do this instead String s = “string”;String appending, Boxed primitives
  • Prefer primitives toboxed primitives, and watch out for unintentional autoboxing

###Item 6: Eliminate obsolete object references.

  • Although Java has garbage collector, memory leak can still happen. GC reclaims the memory by inspecting the references of objects.
  • The fix for this sort of problem is simple: null out references once they become obsolete
  • Nulling out object references should be the exception rather than the norm.
  • Common source of memory leaks, whenever a class manages its own memory, caches,listeners and other callbacks.
  • Common memory leak scenarios: Objects constructed inside classes, Cache, Callbacks
  • Generally speaking, whenever a class manages its own memory, the pro-grammer should be alert for memory leaks. Whenever an element is freed, any object references contained in the element should be nulled out.
  • The best way to ensure that callbacks are garbage collected promptly is to store only weak refer-ences to them, for instance, by storing them only as keys in a WeakHashMap. ###Item 7: Avoid finalizers

  • Severe performance penalty for using finalizers.
  • No guarantee that it will actually run.

Back     Next
comments powered by Disqus