The Java language supports four types: interfaces, classes, arrays, basic types
A member of a class consists of its domain field, method, member class, member interface.
The signature of the signature method consists of its name and all parameter types, and the signature does not include its return type.
API elements: Class, interface, constructor, member, serialization form.
Chapter II Creation and destruction of objects 7 articles
1th replace the constructor with a static factory method
1 Public Static Boolean ValueOf (boolean b) {23return b? Boolean.TRUE:Boolean.False; 4 5 }
The advantage of this is that
1, the call when there is a method name, can provide more information.
When there are multiple constructor functions with the same parameters, the order of the parameters is better than the other.
1 BigInteger (int,int Random);23 biginteger.makeprime ( int,int Random); // Multi-method, can have more instructions
2. You don't have to create a new object every time you call.
You can let immutable classes use pre-built instances, or cache instance reuse.
You can control that each class is singleton or not instantiated. This makes it possible to use a==b instead of A.equals (b) to improve performance.
3. You can return an object of any subtype of the original return type, with greater flexibility when you choose to return the class.
The API can return an object without making the object's class public. Makes the API concise. The private class does not display, and the interface that uses public calls the implementation of private static. At the same time the implementation of the interface can continue to change, transparent to the caller. Such as:
1 Public Static<T>intBinarySearch (list<?extendsT> list, T key, comparator<?SuperT>c) {2 if(c==NULL)3 returnBinarySearch (list<?extendscomparable<?SuperT>>) list, key);4 5 if(Listinstanceofrandomaccess | | List.size () <binarysearch_threshold)6 returnCollections.indexedbinarysearch (list, key, c);7 Else8 returnCollections.iteratorbinarysearch (list, key, c);9 }Ten One Private Static<T>intIndexedbinarysearch (list<?extendsT> L, T key, comparator<?SuperT> c) {
4, create a parameterized type instance, the code is more concise (now about the generic has been improved)
1 New Hashmap<string,list<string>>()23// improved to 45 public static<K,V> hashmap<k,v> newinstance () {6 7 return New Hashmap<k,v>(); 8 9 }
2nd Use Builder when encountering multiple constructor parameters
When a class is constructed with many parameters that need to be passed in, and a few must be removed, there are many optional parameters that can be used to build the object. This is also more common in Google's datastore.
1 //Builder Pattern2 Public classnutritionfacts{3 Private Final intservingsize;4 Private Final intservings;5 Private Final intfat;6 Private Final intsodium;7 8 Public Static classbuilder{9 //Required paramTen Private Final intservings; One Private Final intservingsize; A - //Optional parameters-initialized to default values - Private intFat = 0; the Private intSodium = 0; - - PublicBuilder (intServingsize,intservings) { - This. servingsize =servingsize; + This. servings =servings; - } + PublicBuilder Fat (intval) { AFat =Val; at return This; - } - PublicBuilder Sodium (intval) { -Sodium =Val; - return This; - } in Publicnutritionfacts Build () { - return NewNutritionfacts ( This); to } + } - the Privatenutritionfacts (Builder builder) { *Servingsize =builder.servingsize; $Servings =builder.servings;Panax NotoginsengFat =Builder.fat; -Sodium =Builder.sodium; the } +}
Call:
1 New Nutritionfacts.builder (8). Calories (+). Sodium (+). Build ();
3rd Hardening the Singleton property with a private constructor or enumeration type
"Effective Java" notes