The Equals, Hashcode, and ToString methods of the Java record -57-object

Source: Internet
Author: User

The Hashcode method of object is the most basic, and the Equals and ToString methods use the Hashcode method indirectly.

In general, we rewrite the Hashcode method when overriding the Equals method of a class. The IDE tool Eclipse gives us a quick way to rewrite equals and hashcode, not one of them, but also explains that the two methods are generally rewritten in a single piece.


The Equals method of object:

public boolean equals (Object obj) {return (this = = obj);}

Indicates whether an other object is "equal" to this object.

The Equals method implements an equality relationship on a non-null object reference:

    • Reflexivity: X,x.equals (x) should return true for any non-null reference value.

    • Symmetry: x.equals (y) should return true for any non-null reference value x and Y, if and only if Y.equals (x) returns True.

    • Transitivity: For any non-null reference value 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 value x and Y, multiple calls to X.equals (Y) always return TRUE or always return false, provided that the information used in the Equals comparison on the object has not been modified.

    • X,x.equals (NULL) should return FALSE for any non-null reference value.

The Equals method of object implements the most differentiated equality relationship on the object, that is, for any non-null reference value x and Y, this method returns True if and only if X and Y refer to the same object (x = = Y has a value of true).

Note: When this method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the Hashcode method, which declares that the equality object must have an equal hash code.


object'sHashcode method
public int hashcode ()

Returns the hash code value of the object. This method is supported to improve the performance of a hash table (for example, java.util.Hashtable a hash table provided).

hashCodeThe General agreement is:

    • During Java application execution, if the information used by the object for equals comparison is not modified, the same integer must be returned consistently when the hashcode method is called multiple times on the same object. The integer does not need to be consistent from one execution of an application to another execution of the same application.

    • If two objects are equal according to the equals (object) method, the method that is called for each object in both objects hashCode must produce the same integer result.

    • If equals(java.lang.Object) two objects are not equal according to the method, calling the hashcode method on any of these two objects does not require that a different integer result be generated. However, programmers should be aware 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.


object'sTostring
Public String toString ()

Returns the string representation of the object. Typically, the toString method returns a string that represents this object in text form. The result should be a concise but easy-to-read information expression. It is recommended that all subclasses override this method.

ObjectThe method of the class toString returns a string consisting of the class name (the object is an instance of the class), the at marker " @ ", and an unsigned hexadecimal representation of the hash code for this object. In other words, the method returns a string whose value is equal to:

GetClass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())


The Equals, Hashcode, and ToString methods of the Java record -57-object

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.