Effective JavaPresentation of the Joshua Blochs Book.                                                             www.supi...
Effective Java    The Speaker                         Professional Experiences:                         - Bug Out PC      ...
Effective JavaAgenda   Develop more effectively ?        Creating and Destroying objects        Classes and Interfaces ...
Effective Java                 Develop more effectively ?                 Good practices
Develop more effectively ?Creating and Destroying objects     Item 1: Consider static factory methods instead of          ...
Develop more effectively ?Creating and Destroying objects     Item 1: Consider static factory methods instead of          ...
Develop more effectively ?Creating and Destroying objects     Item 1: Consider static factory methods instead of          ...
Develop more effectively ?Creating and Destroying objects     Item 1: Consider static factory methods instead of          ...
Develop more effectively ?Creating and Destroying objects     Item 1: Consider static factory methods instead of          ...
Develop more effectively ?Creating and Destroying objects     Item 1: Consider static factory methods instead of          ...
Develop more effectively ?Creating and Destroying objects      Item 2: Consider a builder when faced with many            ...
Develop more effectively ?Creating and Destroying objects
Develop more effectively ?Creating and Destroying objects      Item 2: Consider a builder when faced with many            ...
Develop more effectively ?Creating and Destroying objects
Develop more effectively ?Creating and Destroying objects      Item 2: Consider a builder when faced with many            ...
Develop more effectively ?Creating and Destroying objects
Develop more effectively ?Creating and Destroying objects      Item 2: Consider a builder when faced with many            ...
Develop more effectively ?Creating and Destroying objects         Item 5: Avoid creating unnecessary objects.   Is this s...
Develop more effectively ?Creating and Destroying objects         Item 5: Avoid creating unnecessary objects.   Improved ...
Develop more effectively ?Creating and Destroying objects         Item 5: Avoid creating unnecessary objects.   Is this c...
Develop more effectively ?Creating and Destroying objects         Item 5: Avoid creating unnecessary objects.   Improved ...
Develop more effectively ?Creating and Destroying objects         Item 5: Avoid creating unnecessary objects.   Is this c...
Develop more effectively ?Creating and Destroying objects         Item 5: Avoid creating unnecessary objects.   Improved ...
Develop more effectively ?Classes and Interfaces        Item 16: Favor composition over inheritance.   Inheritance is a p...
Develop more effectively ?Classes and Interfaces        Item 16: Favor composition over inheritance.
Develop more effectively ?Classes and Interfaces        Item 16: Favor composition over inheritance.   What this code dis...
Develop more effectively ?Classes and Interfaces        Item 16: Favor composition over inheritance.
Develop more effectively ?Classes and Interfaces        Item 16: Favor composition over inheritance.
Develop more effectively ?Classes and Interfaces        Item 16: Favor composition over inheritance.   Design of the Inst...
Develop more effectively ?Classes and Interfaces
Develop more effectively ?Classes and Interfaces     Item 20: Prefer class hierarchies to tagged classes.   Tagged classe...
Develop more effectively ?Classes and Interfaces     Item 20: Prefer class hierarchies to tagged classes.
Develop more effectively ?Classes and Interfaces     Item 20: Prefer class hierarchies to tagged classes.   Class hierarc...
Develop more effectively ?Methods   Consider the following class :
Develop more effectively ?Methods   Consider the following code :   Do you see the problem ?
Develop more effectively ?Methods        Item 39: Make defensive copies when needed.   To protect the internals of a Peri...
Develop more effectively ?Methods             Item 41: Use overloading judiciously.   What does the following program pri...
Develop more effectively ?Methods             Item 41: Use overloading judiciously.   The program print : “Unknown Collec...
Develop more effectively ?Methods             Item 41: Use overloading judiciously.   What does the following program pri...
Develop more effectively ?Methods             Item 41: Use overloading judiciously.   The program print : [-3, -2, -1] [-...
Develop more effectively ?Methods             Item 41: Use overloading judiciously.   Refrain from overloading methods wi...
Develop more effectively ?General Programming    Item 48: Avoid float and double if exact answers are                     ...
Develop more effectively ?General Programming    Item 48: Avoid float and double if exact answers are                     ...
Develop more effectively ?General Programming    Item 48: Avoid float and double if exact answers are                     ...
Develop more effectively ?General Programming                  Item 55: Optimize judiciously.      "More computing sins ar...
Develop more effectively ?General Programming                  Item 55: Optimize judiciously.   Strive to write good prog...
Effective Java                 The Book                 For all the other tips !
The BookThe original version       Effective Java           Second Edition           Joshua BLOCH      Addison Wesley edit...
The BookThe translated version         Java Efficace     Guide de programmation           Joshua BLOCH  Traduction d’Alexi...
Effective Java                 About the Author                 For all the other tips !
About the AuthorJoshua Bloch                       Chief Java architect at Google.                       Jolt Award winn...
About the AuthorBibliographie Effective Java: Programming Language Guide     ISBN 0201310058, 2001.     Second edition:...
About the AuthorBibliographie Java Puzzlers: Traps, Pitfalls, and Corner Cases     ISBN 032133678X, 2005 (co-authored wi...
About the AuthorBibliographie Java Concurrency in Practice     ISBN 0321349601, 2006 (co-authored with Brian      Goetz,...
Effective JavaThe end
Upcoming SlideShare
Loading in...5
×

Effective Java

2,036

Published on

Published in: Technology

Effective Java

  1. 1. Effective JavaPresentation of the Joshua Blochs Book. www.supinfo.com Copyright © SUPINFO. All rights reserved
  2. 2. Effective Java The Speaker Professional Experiences: - Bug Out PC - Groupe Open - ADF - Atos Worldline - Adullact - Xebia IT - Webpulser - Audaxis Training: - DUT Informatique / Montpellier - Supinfo B3 / Montpellier - Supinfo M1 / Lille - STA Java Lille / Valenciennes Brice Argenson- 59253@supinfo.com Certifications: brice.argenson - SCJP 6 @bargenson - SCWCD 5
  3. 3. Effective JavaAgenda  Develop more effectively ?  Creating and Destroying objects  Classes and Interfaces  Methods  General Programming  The Book  About the Author
  4. 4. Effective Java Develop more effectively ? Good practices
  5. 5. Develop more effectively ?Creating and Destroying objects Item 1: Consider static factory methods instead of constructors.  Public constructor is the normal way for a class to allow a client to obtain an instance of itself.  Another technique is to provide a public static factory method !
  6. 6. Develop more effectively ?Creating and Destroying objects Item 1: Consider static factory methods instead of constructors.  First advantage : They have names ! BigInteger(int bitLength, int certainty, Random rnd) Constructs a randomly generated positive BigInteger that is probably prime, with the specified bitLength. BigInteger.probablePrime(int bitLength, Random rnd) Returns a positive BigInteger that is probably prime, with the specified bitLength.
  7. 7. Develop more effectively ?Creating and Destroying objects Item 1: Consider static factory methods instead of constructors.  Second advantage : They are no required to create a new object each time they’re invoked.
  8. 8. Develop more effectively ?Creating and Destroying objects Item 1: Consider static factory methods instead of constructors.  Third advantage : They can return an object of any subtype of their return type.
  9. 9. Develop more effectively ?Creating and Destroying objects Item 1: Consider static factory methods instead of constructors.  Fourth advantage : They reduce the verbosity of creating parameterized type instances.
  10. 10. Develop more effectively ?Creating and Destroying objects Item 1: Consider static factory methods instead of constructors.  First disadvantage : Classes without public or protected constructors cannot be sub-classed.  Second disadvantage : They are not readily distinguishable from other static methods.
  11. 11. Develop more effectively ?Creating and Destroying objects Item 2: Consider a builder when faced with many constructor parameters.  Static factories and constructors share a limitation: they don’t scale well to large members of optional parameters.  Traditionally, two patterns are used :  Telescoping constructor pattern.  JavaBeans pattern.
  12. 12. Develop more effectively ?Creating and Destroying objects
  13. 13. Develop more effectively ?Creating and Destroying objects Item 2: Consider a builder when faced with many constructor parameters.  Telescoping constructor pattern :  Hard to write.  Harder to read :
  14. 14. Develop more effectively ?Creating and Destroying objects
  15. 15. Develop more effectively ?Creating and Destroying objects Item 2: Consider a builder when faced with many constructor parameters.  JavaBeans pattern :  Easier to write.  Easier to read :  But JavaBean may be in an inconsistent state partway through its construction !
  16. 16. Develop more effectively ?Creating and Destroying objects
  17. 17. Develop more effectively ?Creating and Destroying objects Item 2: Consider a builder when faced with many constructor parameters.  Builder pattern :  Easy to write.  Easy to read :  Simulates named optional parameters.  Consistent state control.
  18. 18. Develop more effectively ?Creating and Destroying objects Item 5: Avoid creating unnecessary objects.  Is this statement correct ?
  19. 19. Develop more effectively ?Creating and Destroying objects Item 5: Avoid creating unnecessary objects.  Improved version :
  20. 20. Develop more effectively ?Creating and Destroying objects Item 5: Avoid creating unnecessary objects.  Is this code correct ?
  21. 21. Develop more effectively ?Creating and Destroying objects Item 5: Avoid creating unnecessary objects.  Improved version (about 250 times faster for 10 million invocations) :
  22. 22. Develop more effectively ?Creating and Destroying objects Item 5: Avoid creating unnecessary objects.  Is this code correct ?
  23. 23. Develop more effectively ?Creating and Destroying objects Item 5: Avoid creating unnecessary objects.  Improved version (6.3 times faster) :
  24. 24. Develop more effectively ?Classes and Interfaces Item 16: Favor composition over inheritance.  Inheritance is a powerful way to achieve code reuse  But not always the best !  Inheritance from ordinary concrete classes across package boundaries is dangerous ! Unlike method invocation, inheritance violates encapsulation.
  25. 25. Develop more effectively ?Classes and Interfaces Item 16: Favor composition over inheritance.
  26. 26. Develop more effectively ?Classes and Interfaces Item 16: Favor composition over inheritance.  What this code display ?
  27. 27. Develop more effectively ?Classes and Interfaces Item 16: Favor composition over inheritance.
  28. 28. Develop more effectively ?Classes and Interfaces Item 16: Favor composition over inheritance.
  29. 29. Develop more effectively ?Classes and Interfaces Item 16: Favor composition over inheritance.  Design of the InstrumentedSet is extremely flexible :  Implement the Set interface.  Receive an argument also of type Set.  With inheritance, we could work only with HashSet.  With composition, we can work with any Set implementation !
  30. 30. Develop more effectively ?Classes and Interfaces
  31. 31. Develop more effectively ?Classes and Interfaces Item 20: Prefer class hierarchies to tagged classes.  Tagged classes are verbose, error-prone, inefficient and just a pallid imitation of a class hierarchy.  Here is the class hierarchy corresponding to the original class :
  32. 32. Develop more effectively ?Classes and Interfaces Item 20: Prefer class hierarchies to tagged classes.
  33. 33. Develop more effectively ?Classes and Interfaces Item 20: Prefer class hierarchies to tagged classes.  Class hierarchies are more flexible and provide better compile-time type checking.  Suppose we need a square shape :
  34. 34. Develop more effectively ?Methods  Consider the following class :
  35. 35. Develop more effectively ?Methods  Consider the following code :  Do you see the problem ?
  36. 36. Develop more effectively ?Methods Item 39: Make defensive copies when needed.  To protect the internals of a Period instance from this sort of attack:  You must make defensive copy of each mutable parameter to the constructor !
  37. 37. Develop more effectively ?Methods Item 41: Use overloading judiciously.  What does the following program prints ?
  38. 38. Develop more effectively ?Methods Item 41: Use overloading judiciously.  The program print : “Unknown Collection” three times.  The choice of which overloading to invoke is made at compile time !  Not like overridden methods…
  39. 39. Develop more effectively ?Methods Item 41: Use overloading judiciously.  What does the following program prints ?
  40. 40. Develop more effectively ?Methods Item 41: Use overloading judiciously.  The program print : [-3, -2, -1] [-2, 0, 2].  The remove method is overloaded inside List class :  remove(E) : delete the E element inside the list.  remove(int) : delete the element at the specified position.
  41. 41. Develop more effectively ?Methods Item 41: Use overloading judiciously.  Refrain from overloading methods with multiple signatures that have the same number of parameters.  If you can’t, at least avoid situations where the same set of parameters can be passed to different overloadings by the addition of casts.
  42. 42. Develop more effectively ?General Programming Item 48: Avoid float and double if exact answers are required.  What does the following program prints ?
  43. 43. Develop more effectively ?General Programming Item 48: Avoid float and double if exact answers are required.  It prints you can afford 3 items and you have $0.3999999999999999 left…  The float and double types are designed primarily for scientific and engineering calculations.  They perform binary floating-point arithmetic with approximations !  They should not be used where exact results are required.
  44. 44. Develop more effectively ?General Programming Item 48: Avoid float and double if exact answers are required.  A correct version of the previous program :
  45. 45. Develop more effectively ?General Programming Item 55: Optimize judiciously. "More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity." - W.A. Wulf “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald E. Knuth “The First Rule of Program Optimization: Dont do it. The Second Rule of Program Optimization (for experts only!): Dont do it yet.” - Michael A. Jackson
  46. 46. Develop more effectively ?General Programming Item 55: Optimize judiciously.  Strive to write good programs rather than fast ones.  Strive to avoid design decisions that limit performance.  Consider the performance consequence of your API design decisions.  Measure performance before and after each attempted optimization.
  47. 47. Effective Java The Book For all the other tips !
  48. 48. The BookThe original version Effective Java Second Edition Joshua BLOCH Addison Wesley editions "I sure wish I had this book ten years ago. Some might think that I dont need any Java books, but I need this one.” - James Gosling, Fellow and Vice President, Sun Microsystems, Inc.
  49. 49. The BookThe translated version Java Efficace Guide de programmation Joshua BLOCH Traduction d’Alexis Moussine-Pouchkine Vuilbert editions http://www.amazon.fr/Java-efficace-Bloch/dp/2711748057/ref=sr_1_1?ie=UTF8&s=books&qid=12985674 25&sr=8-1
  50. 50. Effective Java About the Author For all the other tips !
  51. 51. About the AuthorJoshua Bloch  Chief Java architect at Google.  Jolt Award winner.  Previously a distinguished engineer at Sun Microsystems and a senior systems designer at Transarc.  Led the design and implementation of numerous Java platform features. http://googleresearch.blogspot.com/ @joshbloch
  52. 52. About the AuthorBibliographie Effective Java: Programming Language Guide  ISBN 0201310058, 2001.  Second edition: ISBN 978-0-321-35668-0, 2008.
  53. 53. About the AuthorBibliographie Java Puzzlers: Traps, Pitfalls, and Corner Cases  ISBN 032133678X, 2005 (co-authored with Neal Gafter).
  54. 54. About the AuthorBibliographie Java Concurrency in Practice  ISBN 0321349601, 2006 (co-authored with Brian Goetz, Tim Peierls, Joseph Bowbeer, David Holmes, and Doug Lea).
  55. 55. Effective JavaThe end
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×