Effective Java reading notes

< H3 id = "1 static factory method" > 1 Static factory method

There are three ways: set the constructor method to private and export a final public static member. The constructor method is set to private, and the public static factory method is used to export the private static member of final. The enumeration type containing a single element is functionally similar to the public domain. Advantages: the code is concise, and the serialization mechanism is provided free of charge to prevent multiple serialization. Summary: the enumeration type of single element is the best way to realize singleton, which is jdk1 5 at first. The core idea of the first two methods: set the constructor to private + export public static members

Class manages its own memory solution: once an element is released, any object references contained in the element should be cleared. Example: Stack class, the array storing elements is elementdata. After the pop operation, you should execute elementdata [elementcount] = null; Thus, GC can clean up the objects in which references are saved (the object reference is saved in elementdata, not the object itself). The cache solution cache should regularly clear invalid items. This cleanup can be done by a background thread, which cleans up listeners and other callbacks when adding entries to the cache. Solution: callbacks are immediately treated as garbage collection. The best way is to save only their weak references. Weak reference: objects associated with weak references can only survive until the next garbage collection

The general rule when overriding equals is Reflexivity: objects must be equal to their own symmetry: any two objects must be consistent with the question of whether they are equal. Transitivity: if a and B are equal and B and C are equal, a and C are equal. Consistency: if two objects are equal, they always remain equal unless one of them is modified. Nonempty: all objects must be * not equal to * null. Common error usage: the first sentence of equals methods of many classes is: if (o = = null) return false; But it's redundant. Because the equals method must call the instanceof operator to check whether its parameters are of the correct type before converting the parameters to the appropriate type. Null when checking the type of instanceof, if the incoming parameter is null, it will directly return false. High quality equals () writing principle: use the = = operator to check whether the parameter is a reference to this object. If yes, return true. This is a performance optimization and is worth it if the comparison operation can be expensive. Use the instanceof operator to check whether the parameter is of the correct type. Convert the parameter to the correct type. For each key field in this class, check whether the field in the parameter matches the corresponding field in the object. Looking at the JDK source code, you can find that it is basically written in this way, and the format is completely consistent. Common usage notes: for the basic types of non float and double, you can directly compare them with = =. Object references a domain, the equals method is called recursively. For the float field, use float Cmopare() method. For the double field, use double Compare() method. When overriding equals, it is important to override the hashcode () method, because any two equal objects must return the same integer when calling the hashcode () method.

The biggest difference is to implement the type defined by the abstract class. The class must be a subclass of the abstract class. Interface advantages existing classes can be easily updated to implement new interfaces. For a new interface, if a class wants to implement it, it only needs to add an implementation clause in the class declaration and override the methods in the interface. The ideal choice for defining mixins (mixed types) when defining interfaces. A mixingtype means that a class can implement this mixin type in addition to its "basic type" to indicate that it provides some alternative behavior. For example, clone interface and comparable interface in JKD. If a class implements the comparable interface, it indicates that the instances of this class can be sorted with other comparable objects. Interfaces allow the construction of non hierarchical type frameworks

Different point arrays are covariant and generics are immutable. Covariance: if sub is a subtype of super, the subtype of array type sub [] is super [] immutable: for any two different types type1 and type2, list is neither a subtype of list nor a supertype of list, and the array is materialized. Type of array elements: check type constraint generics only when the program is running: strengthen type information at compile time and erase type information at run time. Why is it illegal to create generic arrays? Because it is not type safe. Because the type check of array elements occurs at run time, if a generic array is defined, its type cannot be checked at compile time. If there is an error in the definition, a ClassCastException exception will occur at program run time. This violates the guarantees provided by generic systems. Because generics are type checked at compile time, ClassCastException will not appear at run time.

The mechanism first creates an array, then passes the parameter value into the array, and finally passes the array to the method performance problem. Each call of variable parameter method will lead to array allocation and initialization.

The main difference is that basic types only have values, while boxed types have different identities from their values. Explanation: in other words, two boxed basic types can have the same function value, but have different identity. The basic type has only fully functional values, but the packing type has a non functional value null in addition to the corresponding functional value. The basic type saves space and time than the packing type. Reasonable use of boxing types as elements, keys, values in a collection – boxing types must be used in parameterized types, and boxing types must be used when calling reflective methods

Definition refers to the special method written by local programming language (such as C or C + +). The purpose provides the ability to "access platform specific mechanism" and the ability to access legacy code base. The disadvantage is that the program can no longer be affected by memory corruption errors. Because the local language is unsafe, the program is no longer freely portable. Because the local language is a platform related program, it is difficult to debug. Entering and exiting the native method requires related expenses. The local method that needs "glue code" is tedious and difficult to read

The biggest cost of serialization is that once a class is published, it greatly reduces the "flexibility" of changing the implementation of the class. If a class implements the serializable interface, its byte stream coding becomes a part of its export API. Once this class is widely used, it is often necessary to always support this serialization form. It increases the possibility of bugs and security vulnerabilities. Because the serialization mechanism is an object creation mechanism other than a language, the deserialization mechanism is a "hidden constructor". Because there is no displayed constructor, it is easy to forget to ensure that the deserialization process must also ensure the "constraint relationship established by the real constructor", And the attacker is not allowed to access the internal information of the object being constructed. As classes release new versions, the associated test burden increases. When a serializable class is revised, it is important to check whether it can "serialize an instance in the new version and then deserialize it in the old version", and vice versa. The disadvantage of using default serialization is that the export API of this class is always bound to the ER internal representation of this class, which consumes too much space and time. Because the serialization logic does not understand the topological relationship of the object graph, it must go through an expensive graph traversal process. Cause stack overflow the default serialization process is to perform a recursive traversal of the object graph, which may cause stack overflow even for medium-sized object graphs.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>