Java-57-Object equals, hashcode, and toString Methods

Source: Internet
Author: User

Java-57-Object equals, hashcode, and toString Methods
The hashCode method of an Object is the most basic. Both the equals and toString Methods indirectly use the hashCode method. Generally, when we override the equals method of a class, the hashCode method is overwritten. The IDE tool eclipse provides us with a quick method to override equals and hashCode, instead of one. It also shows that these two methods are generally overwritten in one piece. The equals method of the Object: public boolean equals (Object obj) {return (this = obj);} indicates whether another Object is "equal" to this Object ". The equals method achieves equality on non-null object references: Inversion: returns true for any non-null reference values x and x. equals (x. Symmetry: For any non-null reference values x and y, x. equals (y) returns true only when y. equals (x) returns true. Passed: For any non-null reference values x, y, and z, if x. equals (y) returns true, and y. equals (z) returns true, then x. equals (z) should return true. Consistency: For any non-null reference values x and y, call x multiple times. equals (y) always returns true or false, provided that the information used in the equals comparison on the object is not modified. Returns false for any non-null reference value x, x. equals (null. The equals method of an Object implements the maximum possible equality relationship between objects. That is, for any non-null reference values x and y, if and only when x and y reference the same Object, this method returns true (x = y has a value of true ). Note: When this method is overwritten, it is usually necessary to override the hashCode method to maintain the conventional protocol of the hashCode method, which declares that equal objects must have equal hash codes. The hashCode method of the Object public int hashCode () returns the hash code value of the Object. This method is supported to improve the performance of hash tables (such as hash tables provided by java. util. Hashtable. The conventional hashCode protocol is that, during Java application execution, if the information used for equals comparison is not modified, when the hashCode method is called for the same object multiple times, the same integer must be returned consistently. This integer does not need to be consistent from one execution of an application to another execution of the same application. If the two objects are equal according to the equals (Object) method, the hashCode method must be called for each Object of the two objects to generate the same integer result. If the two objects are not equal according to the equals (java. lang. Object) method, different integer results are not required to be generated if the hashCode method is called on any of the two objects. However, programmers should realize that generating different integer results for unequal objects can improve the performance of the hash table. In fact, the hashCode method defined by the Object class does return different integers for different objects. The toString public String toString () of the Object returns the String representation of the Object. Generally, the toString method returns a string that "represents the object in text. The result should be a concise but easy-to-understand information expression. We recommend that you override this method for all subclasses. The toString method of the Object class returns a string consisting of the Class Name (the Object is an instance of the class) the at tag "@" and the unsigned hexadecimal representation of the object's hash code. In other words, this method returns a string with the value equal to: getClass (). getName () + '@' + Integer. toHexString (hashCode ())

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.