Java =, equals, hashcode difference and override equals and hashcode Method Instance), implements shashcode

Source: Internet
Author: User

Differences between =, equals, and hashcode in Java and the example of rewriting equals and hashcode (conversion) and mongoshashcode
Java =, equals, hashcode difference and override equals and hashcode method example original address: http://www.cnblogs.com/luankun0214/p/4421770.html

1. Override the equals Method Instance part of the code reference http://blog.csdn.net/wangloveall/article/details/7899948

The purpose of rewriting the equals method is to determine whether the content of the two objects is the same (the content can contain many objects, such as comparing names and ages, and the same object is used ).

If you do not override equals, the purpose of the comparison is to compare whether the object reference points to the same memory address. In particular, equals is used to compare eight packaging objects.
(Such as int, float, etc.) and the String class (because the equals and hashcode methods have been overwritten in this class), the default value is compared, when comparing other custom objects, it is a reference address for comparison.
Package com. lk. c; class User {private String name; private int age; public int getAge () {return age;} public void setAge (int age) {this. age = age;} public void setName (String name) {this. name = name;} public String getName () {return name;} public boolean equals (Object obj) {if (this = obj) {return true ;} if (null = obj) {return false;} if (this. getClass ()! = Obj. getClass () {return false;} User user = (User) obj; if (this. name. equals (user. name) & this. age = user. age) {return true;} return false;} public class Test6 {public static void main (String [] args) {User userA = new User (); userA. setName ("Wang Ming"); userA. setAge (10); User userB = new User (); userB. setName ("Wang Ming"); userB. setAge (10); User userC = new User (); userC. setName ("Wang Liang"); userC. setAge (10); System. out. println ("userA equals userB:" + userA. equals (userB); System. out. println ("userA equals userC:" + userA. equals (userC ));}}
userA equals userB:trueuserA equals userC:false

In Java, what does it mean to rewrite the Hashcode method after the equals method is rewritten?

The reason is as follows: when the equals method is overwritten, it is usually necessary to override the hashCode method to maintain the conventional protocol of the hashCode method. The Protocol declares that an equal object must have an equal hash code. As follows:
(1) When obj1.equals (obj2) is true, obj1.hashCode () = obj2.hashCode () must be true
(2) When obj1.hashCode () = obj2.hashCode () is false, obj1.equals (obj2) must be false

Hashcode is used for quick access to hash data. For example, when using the HashSet/HashMap/Hashtable class to store data, it is determined whether the hashcode value of the stored object is the same.

In this way, if we override euqals for an object, that is, as long as the object's member variables are all equal, euqals is equal to true, but hashcode is not overwritten, then we will create a new object, when the original object. when equals (new object) is equal to true, the hashcode of the two is different, which leads to different understandings.

2. Check the following three procedures.

Package com. lk. c; public class Test7 {public static void main (String [] args) {int a = 10; int B = 10; System. out. print ("basic type a = B:"); System. out. println (a = B); System. out. println ("-----"); String s1 = "abc"; String s2 = "abc"; System. out. print ("String type is s1 = s2:"); System. out. println (s1 = s2); System. out. println ("-----"); String s3 = new String ("abc"); String s4 = new String ("abc "); // We can see that = compares whether the stack address is the same as the System address. out. print ("String type with new String () is s1 = s2:"); System. out. println (s3 = s4); System. out. println (s1 = s3); System. out. println ("-----"); Integer i1 = 1; Integer i2 = 1; System. out. print ("the packaging type is i1 = i2:"); System. out. println (i1 = i2); System. out. println ("-----"); Integer i3 = 128; Integer i4 = 128; // at this time, the output is false because the Integer will be cached between-128-127, and the System will not be cached if it exceeds this range. out. print ("the packaging type is i3 = i4:"); System. out. println (i3 = i4); System. out. println ("-----"); Integer i5 = new Integer ("1"); Integer i6 = new Integer ("1"); System. out. print ("the packaging type is i5 = i6:"); System. out. println (i5 = i6); // System is not cached when new Integer () is used. out. println ("-----"); A a1 = new A (1); A a2 = new A (1); A a3 = a2; System. out. print ("normal reference type a1 = a2:"); System. out. println (a1 = a2); System. out. println (a2 = a3); // The Connection address assigned to the new object is the same System. out. println ("-----") ;}} class A {int I; public A (int I) {this. I = I ;}}
Basic Type a = B: true ----- String type is s1 = s2: true ----- String type is s1 = s2 with new String: falsefalse ----- the packaging type is i1 = i2: true ----- the packaging type is i3 = i4: false ----- the packaging type is i5 = i6 with new Integer: false ----- normal reference type a1 = a2: falsetrue -----
Package com. lk. c; public class Test8 {public static void main (String [] args) {// TODO Auto-generated method stub System. out. println ("basic type does not have the equals method"); System. out. println ("-----"); String s1 = "abc"; String s2 = "abc"; System. out. print ("String type equals method:"); System. out. println (s1.equals (s2); System. out. println ("-----"); String s3 = new String ("abc"); String s4 = new String ("abc "); // We can see that the equals method compares whether the values in the heap are the same System. out. print ("String type new String () equals method:"); System. out. println (s3.equals (s4); System. out. println ("-----"); System. out. print ("Comparison Between String = and new String ():"); System. out. println (s1.equals (s3); System. out. println ("-----"); Integer i1 = 1; Integer i2 = 1; System. out. print ("equals method of the packaging class:"); System. out. println (i1.equals (i2); System. out. println ("-----"); Integer i3 = new Integer (1); Integer i4 = new Integer (1); System. out. print ("new Integer () of the packaging class uses the equals method:"); System. out. println (i3.equals (i4); System. out. println ("-----"); System. out. print ("Comparison Between Integer = assignment and new Integer () Assignment:"); System. out. println (i1.equals (i3); System. out. println ("-----");}}
The basic type does not have the equals method ----- The equals method of the String type: true ----- The equals method of the new String () of the String type: true ----- String is assigned a value using = and new String () comparison of Value assignment: true ----- equals method of the packaging class: true ----- comparison of new Integer () of the packaging class with equals method: true ----- Integer with = and new Integer: true -----
Package com. lk. c; public class Test9 {public static void main (String [] args) {// TODO Auto-generated method stub Student s1 = new Student ("akun", 21 ); student s2 = new Student ("akun", 21); Student s3 = new Student (); Student s4 = new Student (); Student s5 = s1; System. out. print ("normal class Object = non-default structure:"); System. out. println (s1 = s2); System. out. println (s1 = s5); System. out. println ("-----"); System. out. print ("normal class object equals non-Default Construction:"); System. out. println (s1.equals (s2); System. out. println (s1.equals (s5); System. out. println ("-----"); System. out. print ("normal class Object = default structure:"); System. out. println (s3 = s4); System. out. println ("-----"); System. out. print ("normal class object equals Default Construction:"); System. out. println (s3.equals (s4); System. out. println ("-----"); System. out. print ("compare attributes of common objects equals:"); System. out. println (s1.name. equals (s2.name); System. out. print ("compare attributes of common objects =:"); System. out. println (s1.name = s2.name) ;}} class Student {public String name; public int age; public Student () {} public Student (String name, int age) {this. name = name; this. age = age;} public void test () {System. out. println (this. name); System. out. println (this. age );}}
= Non-default constructor of a common Class Object: falsetrue ----- non-default constructor of equals of a common Class Object: falsetrue ----- = default constructor of a common class object: false ----- equals of a common class object default structure: false ----- compare attributes of a common object equals: true compare attributes of a Common Object ==: true

We can see from the above three programs:

1) For =: In simple type (int, etc.), this method can be used for comparison. This type does not have the equals method, and the int value exists in the stack, = compare whether the stack content is the same. In the String type, it is special to use String = ""; in this case, the two identical values use = for comparison. However, if new String () is used, the values are different. Description: When String = "", java checks whether the heap has the same value. If yes, the address of the new object is also assigned the same as that of the old object, therefore, the = comparison will be the same. But new String () opens up two stacks, so the = comparison will not be the same. For the packaging class, such as Integer = "";, there will be cache in-128-127. Please refer to the above program. Other cases are similar to strings.

2) For equals: the String type or the packaging class at that time. For example, when Integer is used, the value in the heap is compared, and Integer is not cached. For normal classes, equals compares the first address of memory, which is the same as = at this time, that is, it compares whether the two sides point to the same object. For details, see Program 3.

All the above programs have been tested in person. Hope to help you.

 

Here are some of the arguments found in Baidu: http://zhidao.baidu.com/link? Url = Response

In java, (1) for string variables, equal compares the content of objects on both sides, so true is returned if the content is the same. As for the "=" you didn't ask, the first address in the memory is compared. Therefore, if it is not the same object, "=" will not return true but false. For example, String s1 = "abc", s2 = "abc"; String s3 = new String ("abc "); string s4 = new String ("abc"); s1 = s2 // true, s1.equals (s2) // true, s3.equals (s3) // true, equal compares content s3 = s4 // false, = compares the first address, so it is false (2) for non-string variables, equals compares the first address of memory, at this time, it is the same as =, that is, compare whether the two sides point to the same object, that is, Sample sa1 = new Sample (); Sample sa2 = new Sample (); sa1.equals (sa2) // false, because it is not the same object. If sa1 = sa2 is added, sa1.equals (sa2) // true

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.