Analysis of = equals hashcode in Java

Source: Internet
Author: User

1. '=' is used to compare whether the values of two variables (basic type and object type) are equal. If the two variables are of the basic type, it is easy, you can simply compare the values. If the two variables are of the object type, they are still compared, but they compare the references (addresses) of the two objects in the stack ).
Objects are placed in the heap, And the stack stores object references (addresses ). We can see that '=' is used to compare the values in the stack. To compare whether the contents of objects in the heap are the same, rewrite the equals method.
2. The equals method in the object class is compared with '='. Therefore, if the equals method is not rewritten, equals and = are equivalent.
We usually rewrite the equals method to make equals compare the content of two objects, instead of comparing the reference (address) of the object) because we often think that the content of the Compare object is more meaningful than the reference (address) of the Compare object.
3. The hashcode in the object class is an int value converted from the address in the memory of the returned object (which can be viewed as an address ). Therefore, if the hashcode method is not rewritten, The hashcode of any object is not equal. Generally, the hashcode and equals methods need to be rewritten in the Collection class, because if you need to add an object to the Collection class (such as hashset, before adding the object, you need to check whether the object already exists in the set. A better way is to use hashcode.
4. Note that the equals and hashcode methods have been rewritten for classes such as string, integer, Boolean, and double. The two Methods Compare and calculate hashcode Based on the object content. (For details, you can view the source code of string. Java in JDK). As long as the basic type value of the object is the same, the hashcode must be the same.
5. equals () equals two objects, hashcode () is generally equal, it is best to override the hashcode () method when rewriting equals () method; equals () is not equal to the two objects, but it cannot prove that their hashcode () is not equal. In other words, hashcode () may be equivalent to two objects whose equals () method is not equal. In turn, hashcode () is not equal. Equals () is always available. hashcode () is equal. Equals () may be equal or not. In the object class, the hashcode () method is a local method, and the reference (address value) of the object is returned, while the equals () in the object class () the method compares the reference (address value) of two objects. If equals () is equal, the address values of the two objects are equal. Of course, hashcode () is equal.
The following is the test code.

1 public class equals_hashcode {2 public static void main (string [] ARGs) {3 string a = new string ("str "); 4 string B = new string ("str"); 5 system. out. println (A = B); 6 system. out. println (. equals (B); 7 system. out. println (. hashcode (); 8 system. out. println (B. hashcode (); 9 // outputs false true 114225 11422510 Class A {11 string STR; 12 int I; 13 public a (string STR, int I) {14 super (); 15 this. STR = STR; 16 this. I = I; 17} 18} 19 A AA = new A ("str", 1); 20 a BA = new A ("str", 1); 21 system. out. println (AA = BA); 22 system. out. println (AA. equals (BA); 23 system. out. println (AA. hashcode (); 24 system. out. println (BA. hashcode (); 25 // outputs False false 6413875 2117445926 Class B {27 string STR; 28 public B (string Str) {29 This. STR = STR; 30} 31} 32 B AB = new B ("str"); 33 B bb = new B ("str"); 34 system. out. println (AB = bb); 35 system. out. println (AB. equals (bb); 36 system. out. println (AB. hashcode (); 37 system. out. println (BB. hashcode (); 38 // outputs False false 827574 1751056739 class c {40 int I; 41 public C (int I) {42 this. I = I; 43} 44} 45 C AC = new C (1); 46 C BC = new C (1); 47 system. out. println (AC = BC); 48 system. out. println (AC. equals (BC); 49 system. out. println (AC. hashcode (); 50 system. out. println (BC. hashcode (); 51 // outputs False false 27744459 2873739652} 53 54} 55

 

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.