摘要:The Enum specification has this to say about ordinal: “Most programmers will have no use for this method. It is designed for use by general-purpose enum based data structures such as EnumSet and EnumMap.” Unless you are writing such a data structure, you are best off avoiding the ordinal method entirely. 阅读全文
Effective Java 31 Use instance fields instead of ordinals
2014-03-25 19:40 by 小郝(Kaibo Hao), 448 阅读, 0 推荐, 收藏, 编辑Effective Java 30 Use Enums instead of int constants
2014-03-24 20:13 by 小郝(Kaibo Hao), 634 阅读, 0 推荐, 收藏, 编辑
摘要:Enums are far more readable, safer, and more powerful than int constants. Enums can benefit from associating data with each constant sand providing methods whose behavior is affected by this data. When enums benefit from assocating multiple behaviors with a single method perfer constant-specific methods to enums that switch on their own values. Consider the strategy enum pattern if multiple enum constants share common behaviors. 阅读全文
Effective Java 29 Consider typesafe heterogeneous containers
2014-03-23 08:38 by 小郝(Kaibo Hao), 561 阅读, 0 推荐, 收藏, 编辑
摘要:The normal use of generics, exemplified by the collections APIs, restricts you to a fixed number of type parameters per container. You can get around this restriction by placing the type parameter on the key rather than the container. You can use Class objects as keys for such typesafe heterogeneous containers. A Class object used in this fashion is called a type token. You can also use a custom key type. For example, you could have a Database Rowtype representing a database row (the container), 阅读全文
Effective Java 28 Use bounded wildcards to increase API flexibility
2014-03-22 22:06 by 小郝(Kaibo Hao), 607 阅读, 0 推荐, 收藏, 编辑
摘要:Using wildcard types in your APIs, while tricky, makes the APIs far more flexible. If you write a library that will be widely used, the proper use of wildcard types should be considered mandatory. Remember the basic rule: producer-extends, consumer-super(PECS). And remember that all comparables and comparators are consumers. 阅读全文
Effective Java 27 Favor generic methods
2014-03-21 08:16 by 小郝(Kaibo Hao), 446 阅读, 0 推荐, 收藏, 编辑
摘要:Generic methods, like generic types, are safer and easier to use than methods that require their clients to cast input parameters and return values. Like types, you should make sure that your new methods can be used without casts, which will often mean making them generic. And like types, you should generify your existing methods to make life easier for new users without breaking existing clients (Item 23). 阅读全文
Effective Java 26 Favor generic types
2014-03-20 06:50 by 小郝(Kaibo Hao), 460 阅读, 0 推荐, 收藏, 编辑
摘要:Generic types are safer and easier to use than types that require casts in client code. When you design new types, make sure that they can be used without such casts. This will often mean making the types generic. Generify your existing types as time permits. This will make life easier for new users of these types without breaking existing clients (Item 23). 阅读全文
Effective Java 25 Prefer lists to arrays
2014-03-19 08:57 by 小郝(Kaibo Hao), 456 阅读, 0 推荐, 收藏, 编辑
摘要:Arrays are covariant and reified; generics are invariant and erased. As a consequence, arrays provide runtime type safety but not compile-time type safety and vice versa for generics. If you find yourself mixing them and getting compile-time errors or warnings, your first impulse should be to replace the arrays with lists. 阅读全文
Effective Java 24 Eliminate unchecked warnings
2014-03-18 19:34 by 小郝(Kaibo Hao), 521 阅读, 0 推荐, 收藏, 编辑
摘要:Unchecked warnings are important. Don't ignore them. Every unchecked warning represents the potential for a ClassCastException at run-time. Do your best to eliminate these warnings. If you can't eliminate an unchecked warning and you can prove that the code that provoked it is typesafe, suppress the warning with an @SuppressWarnings("unchecked") annotation in the narrowest possible scope. Record the rationale for your decision to suppress the warning in a comment. 阅读全文
Effective Java 23 Don't use raw types in new code
2014-03-17 10:42 by 小郝(Kaibo Hao), 795 阅读, 0 推荐, 收藏, 编辑
摘要:Raw types can lead to exceptions at runtime, so don’t use them in new code; Set<Object> is a parameterized type representing a set that can contain objects of any type; Set<?> is a wildcard type representing a set that can contain only objects of some unknown type, and Set is a raw type, which opts out of the generic type system. The first two are safe and the last is not. 阅读全文
Effetive Java 22 Favor static member classes over nonstatic
2014-03-16 09:53 by 小郝(Kaibo Hao), 608 阅读, 0 推荐, 收藏, 编辑
摘要:If a nested class needs to be visible outside of a single method or is too long to fit comfortably inside a method, use a member class. If each instance of the member class needs a reference to its enclosing instance, make it nonstatic; otherwise, make it static. Assuming the class belongs inside a method, if you need to create instances from only one location and there is a preexisting type that characterizes the class, make it an anonymous class; otherwise, make it a local class. 阅读全文
Effective Java 21 Use function objects to represent strategies
2014-03-15 20:41 by 小郝(Kaibo Hao), 438 阅读, 0 推荐, 收藏, 编辑
摘要:In the Java the function pointers is implemented by the declaring an interface to represent strategy and a class that implements this interface for each concrete strategy. It is possible to define an object whose methods perform operations on other objects, passed explicitly to the methods. An instance of a class that exports exactly one such method is effectively a pointer to that method. 阅读全文
Effective Java 20 Prefer class hierarchies to tagged classes
2014-03-15 09:56 by 小郝(Kaibo Hao), 355 阅读, 0 推荐, 收藏, 编辑
摘要:If you're tempted to write a class with an explicit tag field, think about whether the tag could be eliminated and the class replaced by a hierarchy. When you encounter an existing class with a tag field, consider refactoring it into a hierarchy. 阅读全文
Effective Java 19 Use interfaces only to define types
2014-03-14 08:06 by 小郝(Kaibo Hao), 448 阅读, 0 推荐, 收藏, 编辑
摘要:The constant interface pattern is a poor use of interfaces.
That a class uses some constants internally is an implementation detail. Implementing a constant interface causes this implementation detail to leak into the class's exported API.
It represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility. 阅读全文
Effective Java 18 Prefer interfaces to abstract classes
2014-03-13 09:56 by 小郝(Kaibo Hao), 537 阅读, 0 推荐, 收藏, 编辑
摘要:Advantages of Interfaces
Existing classes can be easily retrofitted to implement a new interface.
Interfaces are ideal for defining mixins.
Interfaces allow the construction of nonhierarchical type frameworks. (Composite) 阅读全文
Effective Java 17 Design and document for inheritance or else prohibit it
2014-03-12 11:21 by 小郝(Kaibo Hao), 549 阅读, 0 推荐, 收藏, 编辑
摘要:Principles
The class must document its self-use of overridable methods.
A class may have to provide hooks into its internal workings in the form of judiciously chosen protected methods.
The only way to test a class designed for inheritance is to write subclasses. You must test your class by writing subclasses before you release it.
Constructors must not invoke overridable methods. This happens when there is a method which can be override by subclass calls the subclass's const 阅读全文
Effective Java 16 Favor composition over inheritance
2014-03-11 23:15 by 小郝(Kaibo Hao), 529 阅读, 0 推荐, 收藏, 编辑
摘要:Inheritance disadvantage
Unlike method invocation, inheritance violates encapsulation. Since you don't know the super class implementation which may involve some unpredictable calling between different methods of super class. And it is difficult to maintain the subclass when there is a change of the super class. 阅读全文
Effective Java 15 Minimize mutability
2014-03-10 17:24 by 小郝(Kaibo Hao), 675 阅读, 0 推荐, 收藏, 编辑
摘要:Use immutable classes as much as possible instead of mutable classes.
Advantage
Easy to design, implement and use than mutable classes.
Less prone to error and more secure.
Immutable objects are simple.
Immutable objects are inherently thread-safe; they require no synchronization.
Immutable objects and their internals can be shared freely between threads.
Immutable calss can provide static factories that cache frequently requested instances to avoid creating new in 阅读全文
Effective Java 14 In public classes, use accessor methods, not public fields
2014-03-09 11:50 by 小郝(Kaibo Hao), 436 阅读, 0 推荐, 收藏, 编辑
摘要:Principle
To offer the benefits of encapsulation you should always expose private field with public accessor method. 阅读全文
Effective Java 13 Minimize the accessibility of classes and members
2014-03-08 09:24 by 小郝(Kaibo Hao), 418 阅读, 0 推荐, 收藏, 编辑
摘要:Information hiding is important for many reasons, most of which stem from the fact that it decouples the modules that comprise a system, allowing them to be developed, tested, optimized, used, understood, and modified in isolation. 阅读全文
Effective Java 12 Consider implementing Comparable
2014-03-07 12:59 by 小郝(Kaibo Hao), 396 阅读, 0 推荐, 收藏, 编辑
摘要:Implementation Key point
• The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x))for all x and y. (This implies that x.compareTo(y)must throw an exception if and only if y.compareTo(x)throws an exception.)
• The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0)implies x.compareTo(z) > 0.
• Finally, the implementor must ensure that x.compareTo(y) == 0 implies that
sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for all 阅读全文