Java EE 8

Source: Internet
Author: User

The Hashcode method Returns the hash code value of the object. This method is supported to provide some advantages to the hash table, for example, the Hashtable provided by Java.util.Hashtable.

The general agreement of Hashcode is:
During Java application execution, when the Hashcode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used in the Equals comparison on the object has not been modified. 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, calling the Hashcode method on each object in two objects must produce the same integer result.
The following conditions are not required: If two objects are not equal according to the Equals (Java.lang.Object) method, then calling the Hashcode method on any of the two objects must produce a different integer result. However, programmers should know 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. (This is typically done by converting the object's internal address to an integer, but the JAVATM programming language does not require this implementation technique.) When the Equals 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.

The equals method , which is inherited by the string class from its superclass object, is used to detect whether two objects are equal, that is, whether the contents of two objects are equal and case-sensitive.

Used to compare references and compare basic data types with different features:

Compare basic data types, if two values are the same, the result is true

When comparing references, the result is true if the reference points to the same object in memory

The hashcode is used for lookups, while equals is used to compare the equality of two objects.

Java EE 8

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.