Wednesday, 6 February 2008

Effective Java by Bloch - Second

  • Immutable object are thread-safe without need of synchronization
  • Using immutable objects is costly if we change it frequently because every time new object is created. So think about using StringBuffer over String and BitSet over BigInteger
  • Immutable classes are recommended to be final or methods to be final, what allow changes in new releases.
  • For serialization if any field in immutable object is mutable then you have to provide method readObject or readResolve
  • Think it over whether choose composition pattern or inheritance
  • Documentation of methods to be overriiden in subclass should have comment staring with: This implementation ... It should describe hot it uses other methods and how their overriding would affect results.
  • Constructors must not involve overridden methods through constructors sequence called.
  • Neither clone nor readObject may invoke overridden methods directly or indirectly.
  • To eliminate different usage of method in class and subclass you can use private helper methods and prepare methods to be overrridden to use this helper method.
  • Common usage of an anonymous class is to create object such as a Thread, Runnable, TimerTask
  • Decision which overloaded method is chosen is made on compile-time. (not runtime)
  • Overriding methods is made on run-time.
  • For money calculation use BigDecimal, int or long
  • Rather use standard exceptions like IllegalArgumentException, IllegalStateException, NullPointerException, IndexOutOfBoundsException, ConcurrentModificationException, UnsupportedOperationException, ArithmeticExceptioln, NumberFormatException.
  • Primitive variables long and double are not written or read atomically.
  • Volatile attribute on variable guarantees that any thread will see the most recently written value
  • Static fields in private static classes are initialized on first use
  • Never invoke wait outside a loop.