Wednesday, June 3, 2009

JavaOne 2009: Effective Java

Joshua Bloch in "Effective Java: still effective after all these years"
First Edition of the famous book Effective Java was in 2001 and is now on its second edition from 2008 and it has new chapters on generics, enums, annotations, varargs, concurrency and Serialization

  • About Generics

    • Unlike arrays, generics types are invariant: List <String> is not a subtype of List <Object>; Wildcard provide additional API flexibility

    • PECS - Producer extends, Consumer super (Mnemonic for Wildcard usage)

    • How to write a container with an arbitrary number of type parameters: typesafe heterogenous container pattern - parameterize selector instead of container; present selector to container to get data; data is strongly typed at compile time; allows for unlimited type parameters

  • Enum Types

    • double temp = thermometer.getTemp(true)

    • double temp = thermometer.getTemp(Temperature.FAHRENHEIT)

    • double temp = thermometer.getTemp(FAHRENHEIT)

    • which of the above would you prefer? :)

  • Synchronized collections are largely obsolete; use ConcurrentHashMap and friends and Never sinchronize on a concurrent Collection

  • The Serialization Proxy Pattern - don't serialize instances of your class; instead, serialize instances of a small, struct-like class that concisely represents it, then reconstitue instances of your class at read time using a constructor that uses only its public methods - Sun uses this pattern on its EnumSet there is a private static final SerializationProxy

The presentation is based on lots and lots examples of code, so it's kind of hard to resume them here! ;) In essence, it is probably a good idea to buy the new edition of the book! ;)

No comments: