高效Java摘要

Effective Java

• Creating and Destroying Objects创建和销毁对象

1. Consider static factory methods instead of constructors

考虑用静态工厂方法代替构造器

2. Consider a builder when faced with many constructor parameters

遇到多个构造器参数时要考虑用构建器

3. Enforce the singleton property with a private constructor or an enum type

用私有构造器或者枚举类型强化singleton属性

4. Enforce noninstantiability with a private constructor

通过私有构造器强化不可实例化的能力

5. Avoid creating unnecessary objects

避免创建不必要的对象

6. Eliminate obsolete object references

消除过期的对象引用

7. Avoid finalizers

避免使用终结函数

 

• Methods Common to All Objects对于所有对象都通用的方法

8. Obey the general contract when overriding equals

改写equals时请遵守通用约定

9. Always override hashCode when you override equals

改写equals时总要改写hashCode

10. Always override toString

始终要改写tostring

11. Override clone judiciously

谨慎地改写clone

12. Consider implementing Comparable

考虑实现Comparable接口

 

• Classes and Interfaces类和接口

13. Minimize the accessibility of classes and members

使类和成员的可访问性最小化

14. In public classes, use accessor methods, not public fields

在公有类中使用访问方法而非公有域

 

15. Minimize mutability

使非可变性最小化

16. Favor composition over inheritance

复合优先于继承

17. Design and document for inheritance or else prohibit it

要么为继承而设计,并提供文档说明,要么就禁止继承

18. Prefer interfaces to abstract classes

接口优于抽象类

19. Use interfaces only to define types

接口只用于定义类型

20. Prefer class hierarchies to tagged classes

类层次优于标签类

21. Use function objects to represent strategies

用函数对象表示策略

22. Favor static member classes over nonstatic

优先考虑静态成员类

 

• Generics泛型

23. Don’t use raw types in new code

请不要在新代码中使用原生态类型

24. Eliminate unchecked warnings

消除非受检警告

25. Prefer lists to arrays

列表优先于数组

26. Favor generic types

优先考虑泛型

27. Favor generic methods

优先考虑泛型方法

28. Use bounded wildcards to increase API flexibility

利用有限制通配符来提升API的灵活性

29. Consider typesafe heterogeneous containers

优先考虑类型安全的异构容器

 

• Enums and Annotations枚举和注解

30. Use enums instead of int constants

用enum代替int常量

31. Use instance fields instead of ordinals

用实例域代替序数

32. Use EnumSet instead of bit fields

用EnumSet代替位域

33. Use EnumMap instead of ordinal indexing

用EnumMap代替序数索引

34. Emulate extensible enums with interfaces

用接口模拟可伸缩的枚举

35. Prefer annotations to naming patterns

注解优先于命名模式

36. Consistently use the Override annotation

坚持使用override注解

37. Use marker interfaces to define types

用标记接口定义类型

 

• Methods方法

38. Check parameters for validity

检查参数的有效性

39. Make defensive copies when needed

必要时进行保护性拷贝

40. Design method signatures carefully

谨慎设计方法签名

41. Use overloading judiciously

慎用重载

42. Use varargs judiciously

慎用可变参数(varargs)

43. Return empty arrays or collections, not nulls

返回零长度的数组或者集合,而不是null

44. Write doc comments for all exposed API elements

为所有导出的API元素编写文档注释

 

• General Programming通用程序设计

45. Minimize the scope of local variables

将局部变量的作用域最小化

46. Prefer for-each loops to traditional for loops

for-each循环优先于传统的for循环

 

47. Know and use the libraries

了解和使用类库

48. Avoid float and double if exact answers are required

如果需要精确的答案,请避免使用float和double

49. Prefer primitive types to boxed primitives

原语类型优先于装箱的原语类型

50. Avoid strings where other types are more appropriate

如果其他类型更适合,则尽量避免使用字符串

51. Beware the performance of string concatenation

了解字符串连接的性能

52. Refer to objects by their interfaces

通过接口引用对象

53. Prefer interfaces to reflection

接口优先于反射机制

54. Use native methods judiciously

谨慎地使用本地方法

55. Optimize judiciously

谨慎地进行优化

56. Adhere to generally accepted naming conventions

遵守普遍接受的命名惯例

 

• Exceptions异常

57. Use exceptions only for exceptional conditions

只针对异常的条件才使用异常

58. Use checked exceptions for recoverable conditions and runtime exceptions for programming errors

对可恢复的条件使用受检异常,对编程错误使用运行时异常

59. Avoid unnecessary use of checked exceptions

避免不必要地使用受检的异常

60. Favor the use of standard exceptions

尽量使用标准的异常

61. Throw exceptions appropriate to the abstraction

抛出与抽象相对应的异常

62. Document all exceptions thrown by each method

每个方法抛出的所有异常都要有文档

63. Include failure-capture information in detail messages

在细节消息中包含失败-捕获信息

64. Strive for failure atomicity

努力使失败保持原子性

65. Don’t ignore exceptions

不要忽略异常

 

• Concurrency并发

66. Synchronize access to shared mutable data

同步访问共享的可变数据

67. Avoid excessive synchronization

避免过多同步

68. Prefer executors and tasks to threads

executor和task优先于线程

69. Prefer concurrency utilities to wait and notify

并发工具优先于wait和notify

70. Document thread safety

线程安全性的文档化

71. Use lazy initialization judiciously

慎用延迟初始化

72. Don’t depend on the thread scheduler

不要依赖于线程调度器

73. Avoid thread groups

避免使用线程组

 

• Serialization序列化

74. Implement Serializable judiciously

谨慎地实现serializable

75. Consider using a custom serialized form

考虑使用自定义的序列化形式

76. Write readObject methods defensively

保护性地编写readobject方法

77. For instance control, prefer enum types to readResolve

对于实例控制,枚举类型优先于readResolve

78. Consider serialization proxies instead of serialized instances

考虑用序列化代理代替序列化实例

 

 

你可能感兴趣的:(高效Java摘要)