, the number of times the operation gets the same value5. Non-nullability. It is generally not necessary to check null = = obj shown in equals, because if (obj instanceof Integer) {} has done this workA few suggestions for writing equals:1. First Use = = check whether a reference to this object2. Use instanceof to check if the parameter is of the correct type3.equals cover Hashcode (), see Nineth4. Don't let equals be too smart and too complicated5. Do not tell the equals argument to another ty
30th use enum instead of intIn all, there are several advantages to using enum1. Compile-time type safety,2. Can be guaranteed to be the value of their own definition, there is no monthly risk,3. Each enum type has its own namespace4. Enumeration can add arbitrary methods and fields5. The enumeration type is final and cannot be instantiated, that is, the instance is controllable.31st Replace ordinal with instance fieldThat is, do not rely on enumerations to specify their values in order to defin
the data is stored in the type of object, then you need to do a type conversion returntype.cast (Data.get (type)); } Public Static voidMain (string[] args) {Manytypeclass MTC=NewManytypeclass (); Mtc.putdata (String.class, "Cutter_point"); Mtc.putdata (Integer.class, 0XCAB145DE); Mtc.putdata (Class.class, Manytypeclass.class); Mtc.putdata (Manytypeclass.class, MTC); String datastring= Mtc.getdate (String.class); intDatainteger = Mtc.getdate (Integer.class); Classclass); Manytypec
the corresponding set set for(inti = 0; i i) {Herbsbytype[i]=NewHashset(); } //determines the position of the corresponding type collection based on the location of the enumeration type and adds the data to it for(Herbold ho:garden) {//ordinal, this returns the position of the enumeration type at the time of declarationHerbsbytype[ho.gettype (). Ordinal ()].add (HO); } //Traverse for(inti = 0; i i) {//This is a running operation here. /*** But he
First, check the validity of the parameters1, consider the limitations of the parameters, write the restrictions to the document, at the beginning of the method by explicit check to enforce these restrictions.Second, if necessary, a protective copy1. If a class has a mutable component that is obtained or returned from the client, the class must consider a protective copy of these components.2, if the copy cost is high, the class can also trust his client, do not copy protection, but to document
type frame of non-hierarchical structure4, the interface can safely enhance the function of the class.5, by providing an abstract skeleton implementation for the important interface, the advantages of the interface and abstract class together.Abstract classes that define types that allow multiple implementations have a distinct advantage over interfaces: The evolution of an abstract class is much easier than the evolution of an interface.VII. interfaces for defining types only1, the constant in
persistence, it is necessary for this class to implement the Serializable interface. Based on experience, value classes such as date and BigInteger should implement serializable, and most of the collection classes should. A class that represents an activity entity, such as a thread pool, should not implement serializable generally.Classes designed for inheritance should implement the serializable interface as little as possible, and the user's interface should inherit as little as possible from
(); }}Although the elements was taken out of the clone once, but the premise of this approach is that elements is not final.It's normal. However, clone cannot be compatible with immutable field referencing mutable objects.If an element of an array is a reference type, the problem still occurs when an element changes.Here, for example Hashtable, the elements in Hashtable are entry with their inner classes.private static class EntryIf you clone the elements directly as in the stack example, the
unless a party is modified.Rather than the Equals method, this class should be designed to be variable or immutable when thinking about writing a class.If it is immutable, consistency needs to be ensured.With these provisions in mind, here are some suggestions for overriding equals:
The first step uses the "= =" operation to verify that the reference is the same, so that unnecessary comparison operations are done.
Use instanceof to check the type of the parameter.
Check all cri
. arrays and generics have this very different type rule, arrays are synergistic, generics are immutable and can be erased. Therefore, the array provides runtime type safety, but no compile-type security. Generally speaking, there is no good mix between the 2, preferably using a list instead of an array.4. preference is given to generics. generally it takes 2 steps for a class to be generic, declares a generic, and changes all objects to the generic parameter E. Finally, ensure that the conversi
parameter, so the static factory method is actually a type conversion method.
Of
A more concise alternative to valueof, used in Enumset and popular.
GetInstance
The returned instance is described by the parameters of the method, but cannot be said to have the same value as the parameter.
Newinstance
Like getinstance, but newinstance can ensure that every instance returned is different from all other instances
GetType
Like getinstance, but
Item2:consider a builder when faced with many constructor parametersWhen a constructor method has more than one parameter, you can consider using the builder approach for processing.Instance code: Public classnutritionfacts {Private Final intservingsize; Private Final intservings; Private Final intcalories; Private Final intfat; Private Final intsodium; Private Final intcarbohydrate; Public Static classbuilder{//The following two parameters are required Private Final intservingsize; Priv
/** * Returns a 0-length array or collection instead of NULL * * @author Liu Xiangfeng * */ Public class Shop { PrivateList//0 Length array constants are passed to the ToArray method to indicate the expected return type Private Final Staticobject[] Empty_object_array =Newobject[0]; Public Shop(listSuper(); This. objectList = objectList; }/** * * @return An array containing all of the objects in the shop */ PublicObject[]getobjects() {//If the collection is empty, he will use a 0-len
Class can provide a static method that returns a static instance of a class, such as a static method that obtains an instance of a Boolean wrapper class1 Public Static Boolean ValueOf (boolean b) {2 return (b? True:false); 3 }Advantages:1, there is a name. When a class requires more than one constructor with the same signature, the static factory method is used instead of the constructor, and the name is carefully chosen to highlight the difference between them. such as: biginteger.
{Interfacea a=NewInterfacea () {//member Anonymous Class}; Public Static voidMain (string[] args) {Interfacea a=NewInterfacea () {//local Anonymous class};//The above two are implemented by implementing an interface anonymous class, called an interface anonymous class, can also be inherited by the classTest test =NewTest () {};//inherited anonymous Classes//can also be on the parameterNewThread (NewRunnable () {@Override Public voidrun () {}}). Start ();//belonging to a local anonymous class}Pr
Label:The visible field of the access modifier Private-the member is accessible only from the top-level class where it is declared.Package-private-the member is accessible from any class in the packagewhere it is declared. Technically known as default access, this is the access levelYou get if no access modifier is specified.Protected-the member is accessible from subclasses of the class where it isDeclared (subject to a few restrictions [JLS, 6.6.2]) and from any class in thePackage where it is
Tips"Effective Java, third Edition" an English version has been published, the second edition of this book presumably many people have read, known as one of the four major Java books, but the second edition of 2009 published, to now nearly 8 years, but with Java 6, 7, 8, and even 9 of the release, the
Each Java object has a hashcode () and Equals () method. Many classes ignore (Override) default implementations of these methods to provide deeper semantic comparability between object instances. In the Java Philosophy and Practice section, Java Developer Brian Goetz introduces you to the rules and guidelines you should follow when creating
Effective Java (Chinese second edition) "PDF" Download Link:https://u253469.pipipan.com/fs/253469-230382186Java (second edition) "PDF" "title=" "Effective Java (Chinese second edition)" PDF ""/>Editor's recommendation"Sun's core Technology series: EffectiveThe Chinese version of Ja
think in Java , effective Java These two books have always been in the Java ecosystem for enduring. Originally wanted to turn over think in Java this mountain, but read half to give up. Too long, so unbearable, and then big interest, also by too much cumbersome text erased
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.