Object Class in Java

Source: Internet
Author: User

Features of the equals () method of the Object class:

1) Self-inverse: for non-null reference x, x. equals (x) must return true;

2) symmetry: for non-null references to x and y, if x. equals (y) returns true, then y. equals (x) returns true;

3) transmission: for non-null references x, y, and z, if x. equals (y) returns true, and y. equals (z) returns true, then x. equals (z) must return true;

4) Consistency: for non-null references x and y, if x. if true is returned for equals (y), true is returned for the second, third, and n calls to the equals method, the precondition is that during the call, neither x nor y will change, and vice versa;

5) for non-null reference x, x. equals (null) returns false.

Features of the hashCode () method of the Object class:

1) during a Java program running, if the hashCode method is called multiple times for the same object, the returned int value is the same, provided that the object has not changed during multiple calls; the returned value of the hashCode method of an object during this Java program Runtime is different from that of the hashCode method of the object during the previous Java program runtime.

2) If two objects return true after comparison using the equals method, the return value of their hashCode method is the same integer;

3) If two objects are returned as false after comparison using the equals method, the return values of their hashCode method are generally different integers.

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.