Effective Java Chapter III common methods for all objects (i) reading notes

Source: Internet
Author: User

Methods that are common to all objects

Follow common conventions When overriding equals

Each instance of a class is inherently unique.
Does not care if the class provides a logical equivalent test function
The superclass has overridden equals, and the behavior inherited from the superclass is also appropriate for subclasses.
Class is private or package-level private, it can be determined that its Equals method is never called.
throw new Assertionerror ()

General coverage Object.Equals are value classes
However, there is a value class that does not need to overwrite the Equals method, that is, the instance is controlled, ensuring that each value has a class of at most one object. such as enumeration

Overrides the Equals method, a common convention.
Reflexive nature
Symmetry of
Transitivity
Consistency
Non-void sex

The principle of the Richter replacement
Any important property of a type will also apply to its subtypes.

Tips for achieving a high-quality equals method
1. Use the = = operator to check if "parameter is a reference to this object"
2. Use the instanceof operator to check if the parameter is the correct type
3. Convert the parameters to the correct type
4. For each critical field in the class, check that the field in the parameter matches the corresponding field in the object.

Overwrite equals when always overwrite Hashcode
Do not attempt to make the Equals method too intelligent
Do not replace object objects in the equals declaration with other types.

Overwrite equals when always overwrite Hashcode
Conventions:
During application execution, the Hashcode method must consistently return the same integer if the information used for the comparison of the object's Equals method has not been modified, and the same object is called multiple times. During multiple executions of the same application, the integer returned by each execution can be inconsistent.
If the object is equal according to the Equals (object) method, the Hashacode method that calls either object in both objects must produce the same integer result.
If two objects are not equal according to the Equals (object) method, then calling the Hashcode method of any of these two objects does not necessarily produce a different integer result. However, programmers should know that it is possible to improve the performance of a hash table by producing distinct integer results for unequal objects.

Hash.put
Hash.get
Can't get

There is no value in writing a hashcode method that is legitimate but not useful.

In the calculation of the hash code, the redundant domain can be excluded.

Effective Java Chapter III common methods for all objects (i) reading notes

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.