Monday, October 14, 2013

Notes of “Effective Java” (Chapter 7-11)

Item 37: Use marker interfaces to define types
Marker interface contains no method declarations, which indicates some property or features. The typical marker interface is Serialize interface.
Item 38: Check parameters for validity
Check and restrict the parameters of functions.
  • Throw Exception
  • Assert
Item 39: Make defensive copies when needed
In general, if a class has mutable components that it gets from or returns to its clients, the class must defensively copy these components.
Defensive copy usually implemented by:
  • clone() method
  • copy constructor
  • static factory
This is not mandatory. If the class trusts the clients, then defensive copy could be replaced by documentation explanation to save the cost.

Item 40: Design method signatures carefully
Avoid long parameter lists.  Divide them to several methods or use helper method and helper class.
Favor interfaces over classes.
Prefer two-element enum types to boolean parameters for clearness and extendability.
Item 41: Use overloading judiciously
Selection among overloaded methods is static (at the compile time), while selection among overridden methods is dynamic.
Refrain using of overloading methods with same number of parameters.
If you can not avoid that, try avoiding parameters that can be passed to different overloadings by casts.
Item 42: Use varargs judiciously
Do not use varargs unless you really need it and benefit from it.
Varargs is expensive in resources.
Arrays.asList() is tricky. It returns List<Integer> when the argument is Integer[] and returns List<int[]> when the argument is int[].
Item 43: Return empty arrays or collections, not nulls
Returning null requires extra judgement in the client side.
Use new Object1[0] for arrays and emptyList(), emptySet() and emptyMap() for containers.
Item 44: Write doc comments for all exposed API elements
@param tag for the parameters. @return for return values. @throws for exception may throw.
Care the HTML metacharacters in doc comments.
Write succinct and nice comment as the summary description in the beginning.
For methods and constructors: verb phrase .
  • Returns the number of elements in this collection.
For classes, interfaces, and fields: noun phrase.
  • A task that can be scheduled for one-time or repeated execution by a Timer.
Document generic types and constants.
Item 45: Minimize the scope of local variables
Minimize the scope of a local variable by declaring it where it is first used.
Prefer  for loops to  while loops.
Item 46: Prefer for-each loops to traditional for loops
Use the for each loops to iterate the arrays.
  1. you need to remove the elements.
  2. you need to change the value of elements.
  3. special control for the pace of iteration.
Item 47: Know and use the libraries
Be familiar with the contents of java.lang, java.util, and, to a lesser extent,
Item 48: Avoid  float and double  if exact answers are required
Use the BigDecimal or int or long instead.
int for number with less than 9 digits and long for 19 digits.
Item 49: Prefer primitive types to boxed primitives
“==” operation to mixture of primitive and boxed primitive compares the reference identity but not value.
Use autoboxing carefully.
Item 50: Avoid strings where other types are more appropriate
Do not use string type in everywhere.
Item 51: Beware the performance of string concatenation
Use append() of StringBuilder instead.
Item 52: Refer to objects by their interfaces
Using interface as parameter type make the program more flexible.
List<Subscriber> subscribers = new Vector<Subscriber>();
List<Subscriber> subscribers = new ArrayList<Subscriber>();
Item 53: Prefer interfaces to reflection
Reflection loses all information of complile-time check.
Reflection is only used at design time.
When using the class unknown at compile time, try to only instantiate them by reflection. Access fields and call methods using interfaces or superclasses.
Item 54: Use native methods judiciously 
Avoid using native code unless you really need it.
Item 55: Optimize judiciously
Weshould  forget about small efficiencies, say about 97% of the time: prema-ture optimization is the root of all evil.  –Donald E. Knuth.
Write good programs but not fast programs.
Item 56: Adhere to generally accepted naming conventions
Generally, follow The Java Language Specification as conventions.
Package, org.joda.time.format
Class or Interface     Timer, FutureTask, LinkedHashMap, HttpServlet
Method or Field    remove, ensureCapacity, getCrc
Local Variable      i, xref, houseNumber
Type Parameter    T, E, K, V, X, T1, T2
Item 57: Use exceptions only for exceptional conditions
Do not use the exception for control flow, exception is designed for exceptional condition.
Item 58: Use checked exceptions for recoverable conditions and runtime exceptions for programming errors
Checked exception is used in recoverable case. For that, it need to provide information or methods to help the caller to recover.
Item 59: Avoid unnecessary use of checked exceptions
Avoid using the checked exception unless:
  • the exception can not avoided by proper use of API.
  • Handling exception brings benefits.
Item 60: Favor the use of standard exceptions
Some commonly used exceptions:
IllegalArgumentException: Non-null parameter value is inappropriate
IllegalStateException: Object state is inappropriate for method invocation
NullPointerException: Parameter value is null where prohibited
IndexOutOfBoundsException: Index parameter value is out of range
ConcurrentModificationException: Concurrent modification of an object has been detected where it is prohibited
UnsupportedOperationException: Object does not support method
Item 61: Throw exceptions appropriate to the abstraction
If it is impossible to prevent the occurring of exception in lower level, propagate it to higher level by catching and rethrowing.
Try to log the exception information if detailed information is needed.
Item 62: Document all exceptions thrown by each method
Document the exceptions by Javadoc @throws tag. Do not “throws” unchecked exceptions.
Item 63: Include failure-capture information in detail messages
Include failure-capture information in checked exceptions. Those information is useful in recovering from failure.
Item 64: Strive for failure atomicity
Any generated exception should leave the object in the same state it was in prior to the method invocation.
public Object pop() {
if (size == 0)
throw new EmptyStackException();
Object result = elements[--size];
elements[size] = null; // Eliminate obsolete reference
return result;
Do not try to maintain the failure atomicity when throwing errors.
Item 65: Don’t ignore exceptions
Do not use empty catch block to ignore the exception. Throw it outward at least if you do not know how to handle it.
Item 66: Synchronize access to shared mutable data
Synchronization includes two parts: mutual exclusion and visibility. Without synchronization, one thread’s changes might not be visible to other threads.
Do not use  Thread.stop as it is depreciated.
Synchronization has no effect unless both read and write operations are synchronized.
Volatile and Atomic types should be carefully used.
Item 67: Avoid excessive synchronization
Do not call alien methods in synchronized block. Doing this is uncontrollable and may cause exceptions and deadlock.
Use concurrent containers like CopyOnWriteArrayList to separate the data writing and reading. This is particularly useful to the situation in which writing data is happened occasionally.
In a multicore world, the real cost of excessive synchronization is not the CPU time spent obtaining locks; it is the lost opportunities for parallelism and the delays imposed by the need to ensure that every core has a consistent view of memory.
Do not synchronize the class internally unless you have good reason.
Item 68: Prefer executors and tasks to threads
Executor framework separate the task and mechanism of executing. So use executors prior to  Thread.
Using Executors.newFixedThreadPool under heavy loading situations.
Using ScheduledThreadPoolExecutor prior to Timer when multiple timing tasked are required.
Item 68: Prefer executors and tasks to threads
The higher-level utilities in java.util.concurrent fall into three categories:the Executor Framework, concurrent collections; and synchronizers.
Utilities in the concurrent library should be considered first before wait() and notify().
Use  ConcurrentHashMap in preference to  Collections.synchronizedMap  or  Hashtable.
Common synchronizers include CyclicBarrier, CountdownLatch and Semaphore.
Item 70: Document thread safety
The presence of the synchronized modifierin a method declaration is an implementation detail, not a part of its exported API.
To enable safe concurrent use, a class must clearly document what level of thread safety it supports.
levels of thread safety:
  • immutable
  • unconditionally thread-safe
  • conditionally thread-safe
  • not thread-safe
Item 71: Use lazy initialization judiciously
Lazy initialization decreases the cost of initializing a class or creating an instance, at the expense of increasing the cost of accessing the lazily initialized field. It may be worthwhile when only part of instances will be initialized in practice.
Use a synchronized accessor to the getfield method to ensure the concurrency.
Use the lazy initialization holder class idiom for static field.
// Lazy initialization holder class idiom for static fields
private static class FieldHolder {
static final FieldType field = computeFieldValue();
static FieldType getField() { return FieldHolder.field; }
Use the double-check idiom for instance field.
// Double-check idiom for lazy initialization of instance fields
private volatile FieldType field;
FieldType getField() {
FieldType result = field;
if (result == null) { // First check (no locking)
synchronized(this) {
result = field;
if (result == null) // Second check (with locking)
field = result = computeFieldValue();
return result;
Item 72: Don’t depend on the thread scheduler
Do not rely on scheduler for correctness of program.
Do not let the thread busy-wait.
Use Thread.sleep(1) instead of Thread.yield() for increasing concurrency.
Item 73: Avoid thread groups
Thread groups are obsolete.
Item 74: Implement  Serializable judiciously
Long term cost of implementing Serializable include:
Decreases the flexi-bility to change a class’s implementation once it has been released.
Increase the likelihood of bug and security holes.
Increase the burden of testing.
Classes designed for inheritance  should rarely implement Serializable, and interfaces should rarely extend it.
Exceptions: Throwable, Component, and HttpServlet, etc.
Item 75: Consider using a custom serialized form
Use the default serialized form only when an object’s phys-ical representation is identical to its logical content.
Disadvantages of inappropriate default serialized form:
  • permanently ties the exported API to the  current internal representation.
  • consume excessive space.
  • consume excessive time (graph traversal).
  • stack overflow.
Provide writeObject and readOb-ject methods implementing this serialized form.  The transient modifier indicates that an instance field is to be omitted from a class’s default serialized form.
Before deciding to make a field nontransient, convince yourself that its value is part of the logical state of the object.
Declare an explicit serial version UID in every serializable class you write, to tackle the version problem.
Item 76: Write  readObject methods defensively
Make defensive copy of fields in readObject() method is necessary like in the constructor.
For classes with object reference fields that must remain private, defensively copy each object in such a field.
Check any invariants and throw an  InvalidObjectException if a check fails.
Do not invoke any overridable methods in the constructor or readObject() method.
Item 77: For instance control, prefer enum types to readResolve
The instance control (e.g, Singleton) is violated without readResolve() method after serialization.
All instance fields with object reference types must be declared transient if using the readResolve() to do the instance control.
Instance control through Enum is preferred to the readResolve().
Item 78: Consider serialization proxies instead of serialized instances
Serialization proxy pattern is implemented based on an inner static class with a single constructor.
Use writeReplace() of enclosing class to write a proxy instance instead of the instance of enclosing class.
Use readResolve() method to return a instance of enclosing class at the time of deserialization, since the method in the inner class is able to call the constructor of enclosing class.
Two limitations of the serialization proxy pattern:
It is not compatible with classes that are extendable by their clients (in that time, the enclosing class has not been initialized).
It is not compatible with some classes whose object graphs contain circularities.
Serialization proxy pattern is more secure, but with higher expense.


Notes of “Effective Java” (Chapter 1-6)

Item 1: Consider static factory methods instead of constructors
  1. Customized meaningful names better than constructors.
  2. Associated with class instead of creating new objects.
  3. Able to return subtypes.
For the 2nd advantage, static factory methods form the basis of Service provider Framework including service interface, provider interface, provider registration API and service access API.
Item 2: Consider a builder when faced with many constructor parameters
Compare with telescoping constructors and JavaBean Style.
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 patter:
  • Public field
  • Static factory method
  • Enum Singleton
Without Enum, readResovle() function needs to be added to avoid creating spurious instances when doing serialization.
Item 4: Enforce noninstantiability 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
Creating new object especially heavy objects is expensive. Therefore, reusing object always increases efficiency.
Example: String appending, Boxed primitives.
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.
Therefore, we need to null out the obsolete object references to release the unused object memory.
Common memory leak scenarios: Objects constructed inside classes, Cache, Callbacks.
Notice the Weak references and weakHashmap.
Item 7: Avoid finalizers
In summary, don’t use finalizers except as a safety net or to terminate noncritical native resources. In those rare instances where you do use a finalizer, remember to invoke super.finalize.
Explicit termination inside a “try finally” is preferable and reliable.
Item 8: Obey the general contract when overriding equals
Conditions do not need overriding equals():
  1. Instances are distinguished by reference.
  2. Equals() defined in superclass works.
  3. You do not care the equals() function.
Contract when overriding equals():
Reflexive, Symmetric, Transitive, Consistent.
There is no perfect way to override equals() between the subclasses with additional value components and superclasses.
Notice the @Override annotation.
Item 9: Always override hashCode when you override equals
Principle: equal objects according the equals() function must produce the same hashcode.
General recipe for hashCode():
result = 31 * result + c;
  • boolean: (f?1:0).
  •  byte, char, short, or int: (int) f
  • long: (int) (f ^ (f >>> 32))
  • float: Float.floatToIntBits(f)
  • double: Double.doubleToLongBits(f)
  •  null: 0
Note we could lazily initialize the hashCode value.
Item 10: Always override toString
Faciliate the class client user by giving useful information in string.
Item 11: Override clone judiciously
Once one class implements the Cloneable interface and overrides the clone() method, invoking super.clone() will return a field-to-field copy of this class object in which the method is called. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified. Otherwise, deep copy is needed.
Be careful to use clone except for arrays. Usually it is better to use copy constructor and copy factory method.
  • public Yum(Yum yum);
  • public static Yum newInstance(Yum yum);
Item 12: Consider implementing Comparable
public interface Comparable<T> {
int compareTo(T t);
Classes that depend on comparison include the sorted collections TreeSet and TreeMap, and the utility classes Collections and Arrays, which contain searching and sorting algorithms. General interfaces like Set, Map, and Collection depend on equals() method.
If the most significant fields are equal, go on to compare the next-most-significant fields, and so on. If all fields are equal, the objects are equal; return zero.
Item 13: Minimize the accessibility of classes and members
The rule of thumb is simple: make each class or member as inaccessible as possible.
Exported API: public, protected.
If a method overrides a superclass method, it is not permitted to have a lower access level in the subclass than it does in the superclass. This is necessary to ensure that an instance of the subclass is usable anywhere that an instance of the superclass is usable.  A special case of this rule is that if a class implements an interface, all of the class methods that are also present in the interface must be declared public. This is so because all members of an interface are implicitly public.
  • Instance fields should never be public
  • Classes with public mutable fields are not thread-safe
  • Public static final fields should be only used for constants
Item 14: In public classes, use accessor methods, not public fields
Class is package-private or is a private nested class, there is nothing inherently wrong with exposing its data fields.
Item 15: Minimize mutability
  1. Make the immutable class to be final.
  2. Immutable objects are inherently thread-safe; they require no synchroni-zation.
  3. Use companion class such as StringBuilder replacing String for performance.
  4. Static factory is a good way for immutable class.
  5. Classes should be immutable unless there’s a very good reason to make them mutable.
  6. Make every field final unless there is a compelling reason to make it nonfinal.
  7. Don’t provide a public initialization method separate from the constructor or static factory unless there is a compelling reason to do so.
Item 16: Favor composition over inheritance
For most cases, composition is better than inheritance.
The keyword “super” is for calling overrided methods or constructors in superclass. The invocation in super.method() still calls the override methods but not overriden methods.
Item 17: Design and document for inheritance or else prohibit it
  • The class must document or simply prohibit its  self-use of overridable methods.
  • Constructors must not invoke overridable methods.
  • Neither clone() nor readObject() may invoke an overridable method, directly or indirectly, as they behave like constructors.
  • Prohibit subclassing in classes that are not designed and documented to be safely subclassed by “final” or making the constructor “private”.
Item 18: Prefer interfaces to abstract classes
Interface is generally the better way to define a type that permits multiple implementations.
Abstract class and Interface can be combined to generate skeletal implementation, such as AbstractCollection, AbstractSet, AbstractList.
Item 19: Use interfaces only to define types
Interfaces should only used to define type, representing some classes in hierarchy.
Enum and Utility class are more appropriate for defining constants.
Item 20: Prefer class hierarchies to tagged classes
Tagged classes are cluttered with tag fields, and switch statements, messing the encapsulation and  being prone to run-time errors.
Tagged class should be abandoned and replaced by abstract classes.
Item 21: Use function objects to represent strategies
Features like function pointers, delegates, lambda expressions call functions in functions.
Strategy pattern is built on these features typically on function pointers.
Function pointers is not supported in java. However, it can be emulated by a concrete class with only one method, as a “concrete strategy class”.
  • Strategy ==> Interface
  • Concrete strategy ==> Concrete class implementing the interface
Usually the concrete class is used as anonymous class. For class used repeatedly, singleton pattern is applied to the concrete class with one static and final instance.
Item 22: Favor static member classes over nonstatic
Four kinds of nested classes: static member classes, nonstatic member classes, anonymous classes, and local classes.
It is impossible to create an instance of a nonstatic member class without an enclosing instance.
Declare a member class that does not require access to an enclosing instance, always make it to be static.
Three common uses of anonymous classes:
  1. function objects
  2. process objects, such asRunnable, Thread
  3. static factory methods
Item 23: Don’t use raw types in new code
  • Do not use raw type to define variable, as the compiler will not check the type of parameters and it loses the type safety and may generate run-time error.
  • Generics like List<Object> can contain any arbitrary types but still with type safety.
  • Unbounded wildcard types like List<?> is capable to hold one of any type of element with type safety.
  • Generic type information is erased at runtime
Item 24: Eliminate unchecked warnings
Try to eliminate the unchecked warning, including:
  • unchecked cast warnings
  • unchecked method invocation warnings
  • unchecked generic array creation warnings
  • unchecked conversion warnings
Suppress the warning with an @SuppressWarnings(“unchecked”) annotation in narrowest scope if you are sure about the type safety.
Item 25: Prefer lists to arrays
Arrays are covariant and reifiable. Generics are invariant and erased in run-time.
Arrays provide runtime type safety but not compile-time type safety and vice versa for generics.
// Fails at runtime!
Object[] objectArray = new Long[1];
objectArray[0] = "I don't fit in"; // Throws ArrayStoreException
// Won't compile!
List<Object> ol = new ArrayList<Long>(); // Incompatible types
ol.add("I don't fit in");
Generics enforce their type constraints only at compile time and discard (or erase) their element type information at runtime.
it is illegal to create an array of a generic type such as “new List<String>[]” and “new List<E>[]“.
Non-reifiable type is one whose runtime representation contains less information than its compile-time representa-tion.
Casts to arrays of non-reifiable types should be used only under special circumstances. A better solution is to use list.
Item 26: Favor generic types
Generics are safer. Suppress the warning when casting.
public Stack() {
elements = (E[]) new Object[DEFAULT_INITIAL_CAPACITY];
Item 27: Favor generic methods
Generic singleton factory:
// Generic singleton factory pattern
private static UnaryFunction<Object> IDENTITY_FUNCTION =
new UnaryFunction<Object>()
public Object apply(Object arg) { return arg; }
// IDENTITY_FUNCTION is stateless and its type parameter is
// unbounded so it's safe to share one instance across all types.
public static <T> UnaryFunction<T> identityFunction() {
return (UnaryFunction<T>) IDENTITY_FUNCTION;
Recursive type bound is usually used for Comparable<T>.
public interface Comparable<T>; {
int compareTo(T o);
// Using a recursive type bound to express mutual comparability
public static <T extends Comparable<T>> T max(List<T> list) {...}
Item 28: Use bounded wildcards to increase API flexibility
Producer-extends, consumer-super(PECS)
// Wildcard type for parameter that serves as an E producer
public void pushAll(Iterable<? extends E> src) {
for (E e : src)
// Wildcard type for parameter that serves as an E consumer
public void popAll(Collection<? super E> dst) {
while (!isEmpty())
Do not use wildcard types as return types. Rather than providing additional flexibility for your users, it would force them to use wildcard types in client code.
Always use Comparable<? super T> in preference to Comparable<T>, since the type may implements the Comparable interface as an subinterface.
If a type parameter appears only once in a method declaration, replace it with a wildcard.
Can’t put any value except null into a List<?>, which means List of some particular type.
Item 29: Consider typesafe heterogeneous containers
Place the type parameter on the key rather than the container to enable the container to include different types.
// Typesafe heterogeneous container pattern - implementation
public class Favorites {
private Map<Class<?>, Object> favorites =
new HashMap<Class<?>, Object>();
public <T> void putFavorite(Class<T> type, T instance) {
if (type == null)
throw new NullPointerException("Type is null");
favorites.put(type, instance);
public <T> T getFavorite(Class<T> type) {
return type.cast(favorites.get(type));
Item 30: Use enums instead of int constants
Java’s enum types are full-fledged classes.
Enums provide high-quality implementations of all the Object methods, Comparable and Serializable.
Enums can also add methods or fields.
// Enum type with constant-specific method implementations
public enum Operation {
PLUS { double apply(double x, double y){return x + y;} },
MINUS { double apply(double x, double y){return x - y;} },
TIMES { double apply(double x, double y){return x * y;} },
DIVIDE { double apply(double x, double y){return x / y;} };
abstract double apply(double x, double y);
ValueOf(String) method can translate a constant’s name into the constant itself.
Consider the strategy enum pattern if multiple enum constants share common behaviors.
Item 31: Use instance fields instead of ordinals
Avoid using ordinal() method of enum.
// Abuse of ordinal to derive an associated value - DON'T DO THIS
public enum Ensemble {
public int numberOfMusicians() { return ordinal() + 1; }
Item 32: Use EnumSet instead of bit fields
Use general Set interface for input type.
// EnumSet - a modern replacement for bit fields
public class Text {
// Any Set could be passed in, but EnumSet is clearly best
public void applyStyles(Set<Style> styles) { ... }
Item 33: Use EnumMap instead of ordinal indexing
It is better to use EnumMap to catalog relationship related with Enum type.
// Using an EnumMap to associate data with an enum
Map<Herb.Type, Set<Herb>> herbsByType =
new EnumMap<Herb.Type, Set<Herb>>(Herb.Type.class);
for (Herb.Type t : Herb.Type.values())
herbsByType.put(t, new HashSet<Herb>());
for (Herb h : garden)
Item 34: Emulate extensible enums with interfaces
In general, extending enums is a bad idea. If you really want to do that, define an interface for expected behaviors.
Item 35: Prefer annotations to naming patterns
Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. Annotations can be read from source files, class files, or reflectively at run time.
Annotations complement javadoc tags. In general, if the markup is intended to affect or produce documentation, it should probably be a javadoc tag; otherwise, it should be an annotation.
Item 36: Consistently use the Override annotation
Use @Override annotation except for the abstract methods or methods of interfaces.