Practical Java 是本老書 (February 11, 2000) 了,候捷翻譯的中譯本已經絕版了,英文版也不好取得(太貴、要預定),所以我只能從網路上取得(?!),版權之神請原諒我 後來我在露天花了 1 千塊買了侯捷的中文版,這是我第一次看過電腦書會漲價的。 談起這本書,其實有點慚愧,他是一個很基本的書籍,談論 Java 這幾乎是本必讀的書籍,而從大學開始接觸 Java ,但一直都沒有好好把他讀懂,而今年穩定、專注的開始一個關於的 Java 的工作,就想起來好好把這些經典讀一讀。 雖然晚了點,但 It is never too late to learn!
這本書包含了 68 個 praxis, 這個 praxis 字用的就很有意思,從中文的來說,你可以說他是一個習慣、練習,也可以說他一個實踐。在 Practical Java 裡面有的是概念、有的是注意事項,所以我覺得這個 praxis 這個字是用的很到位的。
過去幾年我有認真的讀了這部書的部分章節,記錄下了一些內容。
GENERAL TECHNIQUES.
- Praxis 1: Understand that parameters are passed by value, not by reference.
- Praxis 2: Use final for constant data and constant object references.
- Praxis 3: Understand that all non-static methods can be overridden by default.
- Praxis 4: Choose carefully between arrays and Vectors.
- Praxis 5: Prefer polymorphism to instance of.
- Praxis 6: Use instance of only when you must.
- Praxis 7: Set object references to null when they are no longer needed.
OBJECTS AND EQUALITY.
- Praxis 8: Differentiate between reference and primitive types.
- Praxis 9: Differentiate between == and equals.
- Praxis 10: Do not rely on the default implementation of equals.
- Praxis 11: Implement the equals method judiciously.
- Praxis 12: Prefer get Class in equals method implementations.
- Praxis 13: Call super. equals of base classes.
- Praxis 14: Consider carefully instance of in equals method implementations.
- Praxis 15: Follow these rules when implementing an equals method.
EXCEPTION HANDLING.
- Praxis 16: Know the mechanics of exception control flow.
- Praxis 17: Never ignore an exception.
- Praxis 18: Never hide an exception.
- Praxis 19: Consider the drawback to the throws clause.
- Praxis 20: Be specific and comprehensive with the throws clause.
- Praxis 21: Use finally to avoid resource leaks.
- Praxis 22: Do not return from a try block.
- Praxis 23: Place try/catch blocks outside of loops.
- Praxis 24: Do not use exceptions for control flow.
- Praxis 25: Do not use exceptions for every error condition.
- Praxis 26: Throw exceptions from constructors.
- Praxis 27: Return objects to a valid state before throwing an exception.
PERFORMANCE.
- Praxis 28: Focus initially on design, data structures, and algorithms.
- Praxis 29: Do not rely on compile-time code optimization.
- Praxis 30: Understand runtime code optimization.
- Praxis 31: Use String Buffer, rather than String, for concatenation.
- Praxis 32: Minimize the cost of object creation.
- Praxis 33: Guard against unused objects.
- Praxis 34: Minimize synchronization.
- Praxis 35: Use stack variables whenever possible.
- Praxis 36: Use static, final, and private methods to allow in lining.
- Praxis 37: Initialize instance variables only once.
- Praxis 38: Use primitive types for faster and smaller code.
- Praxis 39: Do not use an Enumeration or an Iterator to traverse a Vector.
- Praxis 40: Use System array copy for copying arrays.
- Praxis 41: Prefer an array to a Vector or Array List.
- Praxis 42: Reuse objects whenever possible.
- Praxis 43: Use lazy evaluation.
- Praxis 44: Optimize source code by hand.
- Praxis 45: Compile to native code.
MULTITHREADING.
- Praxis 46: Understand that for instance methods, synchronized locks objects, not methods or code.
- Praxis 47: Distinguish between synchronized statics and synchronized instance methods.
- Praxis 48: Use private data with an accessor method instead of public or protected data.
- Praxis 49: Avoid unnecessary synchronization.
- Praxis 50: Use synchronized or volatile when accessing shared variables.
- Praxis 51: Lock all objects involved in a single operation.
- Praxis 52: Acquire multiple locks in a fixed, global order to avoid deadlock.
- Praxis 53: Prefer notify All to notify.
- Praxis 54: Use spin locks for wait and notify All.
- Praxis 55: Use wait and notify All instead of polling loops.
- Praxis 56: Do not reassign the object reference of a locked object.
- Praxis 57: Do not invoke the stop or suspend methods.
- Praxis 58: Terminate threads through thread cooperation. Classes and Interfaces.
- Praxis 59: Use interfaces to support multiple inheritance.
- Praxis 60: Avoid method clashes in interfaces.
- Praxis 61: Use abstract classes when it makes sense to provide a partial implementation.
- Praxis 62: Differentiate between an interface, abstract class, and concrete class.
- Praxis 63: Define and implement immutable classes judiciously.
- Praxis 64: Use clone for immutable objects when passing or receiving object references to mutable objects.
- Praxis 65: Use inheritance or delegation to define immutable classes.
- Praxis 66: Call super. clone when implementing a clone method.
- Praxis 67: Do not rely on finalize methods for non-memory resource cleanup.
- Praxis 68: Use care when calling non-final methods from constructors.