java = = and Equals (), Equals (), and Hashcode ()

Source: Internet
Author: User

= = and Equals ()

Remember three words

1. Semantically: = = refers to memory references. equals refers to logical equality. The logical equivalence-specific meaning is determined by the writer.

2. By default(Inherited from the object class), equals and = = are the same, unless it is overwritten (override).
 Public Boolean equals (Object obj) {        return ( this = = obj);    }

3. The most typical equals has been the override example is a string, and string literals in strings are considered logical equal (s1.equals (S2) ==true).

String#equals:

  Public Booleanequals (Object anobject) {if( This==anobject) {            return true; }        if(AnObjectinstanceofString) {String anotherstring=(String) AnObject; intn =value.length; if(n = =anotherString.value.length) {CharV1[] =value; CharV2[] =Anotherstring.value; inti = 0;  while(n--! = 0) {                    if(V1[i]! =V2[i])return false; I++; }                return true; }        }        return false; }

Equals ()

In the beginning of Java, many people will say when comparing objects, = = is the comparative address, equals () is the content of the comparison object, who said?

Look at the definition of the Equals () method in the object class:

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

Is this a comparative content? is obviously the comparison pointer (address) ...

But why is there a statement that equals is the comparative content?

The initial behavior of this method is to compare the memory address of the object, but in some class libraries This method is overridden, such as string,integer,date in these classes where equals has its own implementation, and is no longer a comparison class in the heap memory of the storage address.

Because the comparison of content is done in string.

Hashcode ()

The definition in the object class is:

public native int hashcode ();

is a local method that returns the address value of an object.

However, the same approach is overridden in encapsulation classes such as String. The method call gets an int value from a calculated formula

3. The relationship between the two

① two obj, if equals () is equal, hashcode () must be equal

② two obj, Equals () is not necessarily equal if hashcode () is equal

cause: From the point of view of the hash, different objects can cause conflicts when they calculate hash codes , we must remember the solution of conflict in data structure.

But to design this, with two functions, the individual's understanding is more efficient in order to compare two objects .

Consider that in the Java collection, the rule to determine whether two objects are equal is:

The first step, if Hashcode () is equal, look at the second step, otherwise not equal;

The second step is to see if equals () is equal, and if they are equal, the two obj are equal or not equal.

Why did you do that? The personal understanding is that the proper function is done properly, after all, Hashcode () is somewhat faster than equals ().

(Personal experience, if there is a mistake, please correct me)

java = = and Equals (), Equals (), and Hashcode ()

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.