[Effective Java Reading notes] Chapter III general methods for all objects eighth----? Reviews

Source: Internet
Author: User

This chapter focuses on the methods in the object class, which is the parent class of all classes, so its methods are also called methods that are common to all objects.

Eighth Convention to be followed when overriding equals

The Equals implementation in object is an equal comparison of objects directly:

     Public Boolean equals (Object obj) {        return ( this = = obj);    }

So when do you need to overwrite equals?

When your class has its own logical equality, not object equality, you should implement equals, such as date and Interger, whose equality comparison is not only the equality of objects, but also the equality of the internal values.

//the equals implementation of the integer:     Public Booleanequals (Object obj) {if(objinstanceofInteger) {            returnValue = =((Integer) obj). Intvalue (); }        return false; }//the equals implementation of date:     Public Booleanequals (Object obj) {returnObjinstanceofDate && getTime () = =((Date) obj). GetTime (); }

The implementation of the Equals method must satisfy the equivalence relationship (review the basic concepts of mathematics):

1. Reflexivity. X,x.equals (x) must be true for any non-null reference value

2. Symmetry. For non-null x and Y,x.equals (y) derive the y.equals (x)

3. transitivity. Non-null xyz,x.equals (Y) and Y.equals (z), then X.equals (z)

A typical example is discussed here. Since the class extends the parent class and adds a new value component (variable), it is difficult for equals to take effect at the same time between the parent class subclasses, such as equals of the parent class that lacks the new variable of the subclass, the equals of the subclass to compare the parent class, and the parent class is missing the new variable for the child class.

In this paper, a method is proposed, which is based on the principle of combining precedence over inheritance, the subclass is independent, the component of its value is added, and an instance of the parent class is implemented in the subclass, so that the two parts can be compared with equals at the same time.

4. Consistency. Any non-null XY, as long as the comparison operation of equals does not change the information used in the object, the number of times the operation gets the same value

5. Non-nullability. It is generally not necessary to check null = = obj shown in equals, because if (obj instanceof Integer) {} has done this work

A few suggestions for writing equals:

1. First Use = = check whether a reference to this object

2. Use instanceof to check if the parameter is of the correct type

3.equals cover Hashcode (), see Nineth

4. Don't let equals be too smart and too complicated

5. Do not tell the equals argument to another type, it should be an object

[Effective Java Reading notes] Chapter III general methods for all objects eighth----? Reviews

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.