Article 3: comply with general conventions when rewriting equals

Source: Internet
Author: User

Chapter 1 common methods for all objects

This is what I think is the most useful chapter. I really want to ask myself, are you sure you know the most basic things?
An object is a common and special class. It is common because all classes are derived from it. It is special because it is the parent class of all other classes. You must first understand it before writing your own classes.
All non-final methods of an object are designed for override. There are clear conventions on the rewrite methods. This chapter describes how to rewrite these methods.

 

Article 2: comply with general conventions when rewriting equals

Equals means "equal". It seems that it is easy to rewrite, but it is easy to make mistakes.
Equals rewrite specifications:
1) Self-inverse: X. Equals (x) must be true
2) symmetry: if and only when X. dquals (Y) is true, Y. Equals (x) must also be true.
3) transmission: If X. Equals (Y) is true, Y. Equals (z); then X. Equals (x) must be true.
4) Consistency: For any referenced values X and Y, if the object information used for equals comparison is not modified, the values returned by multiple calls X. dquals (y) are consistent.
5) for non-null reference values X and X. Equals (null), false is returned.
 
It seems that these conventions are all nonsense. A person with no problems in mind will not have any doubts about the above five articles due to their equal understanding. But it is not that simple to implement the Code. It is necessary to use some test code to check the code one by one after rewriting the equals method.

Once one of them is violated, the consequences are unimaginable.

For example, self-defense. If the statement "x. Equals (x) must be true" is violated. When an instance of such a class is put into a collection, when you use the contains method to ask whether the collection contains this instance, the collection will tell you "no ". Why? The contains method is to call the equals method of the object in it to determine whether it is contained (the person who compiled the contains method thinks that all classes should have an equals method meeting the conventions ).

Write the "prescription" of the equals method ":
1) Use the = Operator to check whether the real parameter is a reference to the object"
If (O = This)
Return true;

2) use the instanceof operator to check whether the real parameter is of the correct type"
If (! (O instanceof myclass ))
Return false;

3) convert real parameters to the correct type
Myclass mo = (myclass) O;

4) for each "significant" field in the class, check whether the field in the real parameter matches the field value in the current object.
If (! (This. Field = NULL? O. Field = NULL: This. Equals (O. Field )))
// Or write it as if (! (This. Field = O. FIELD | (this. Field! = NULL & this. Field. Equals (O. Field) for this. Field and O. Field are usually referenced by the same object, which is faster.
Return false;
// Compare the next field
// Comparison is complete
Return true;

5) Finally, confirm the following:
5.1) at the same time of rewriting equals, The hashcode method is always (required) to be rewritten (see [Article 8th]), which is very easy to ignore and is extremely important.
5.2) do not try to make the equals method too clever
5.3) do not use unreliable resources. Such as network connection
5.4) do not replace the object in the equals declaration with another type.
Public Boolean equals (myclass) is not uncommon. It makes it hard for programmers to figure out why they cannot work normally for several hours.
This is because overload is used instead of override (or overwrite)
It is equivalent to adding another overload of the real parameter type myclass to equals. The equals whose real parameter is object is not rewritten, and it is still the original equals inherited from the object, it is impossible for programmers to see what they want. Because the class library or the code written by others call the equals method of the object type with the real parameter (How do others know the myclass you wrote today beforehand ?)

 

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.