The hashcode () and equals () methods in the Java object class are reproduced-----

Source: Internet
Author: User

Java code:
public static void main (string[] Args)
{
Object obj1 = new Object ();
Object obj2= New Object ();
Object obj3 = obj2;
System.out.println ("obj1==obj2?") +obj1.equals (obj2));
System.out.println ("obj2==obj3?") +obj2.equals (obj3));

System.out.println ("obj1 ' s hashcode:" +obj1.hashcode ());
System.out.println ("obj1 ' s hashcode:" +obj2.hashcode ());
System.out.println ("obj1 ' s hashcode:" +obj3.hashcode ());
}
}

Output result:
obj1==obj2? false
obj2==obj3? true
Obj1 ' s hashcode:12677476
Obj1 ' s hashcode:33263331
Obj1 ' s hashcode:33263331

Summarize
Source code in the API object class:
public boolean equals (Object Obj) {
return (this = = obj);
}
That is, when we write our own class, and then use the class new two objects, if the two objects with equals, when the comparison of the two reference is not the same, that is, their physical address is not the same, if not the same, it will return False.
When we actually use it, we often do not want to compare the physical address of two objects is not the same, and compare the properties of two objects and other things are not the same, so object provides methods often do not meet our Requirements.
This requires that we override the Equals method of Object.

If you want to override the Equals method of object, you must satisfy the following equivalence relationships:
1. reflexivity, for any non-null reference value X,x.equals (x) must be reversed back to true
2. symmetry, for any non-null reference value x, y, returns True if and only if Y.equals (x) returns true, x.equals (y).
3. transitivity, for any non-null reference value x, y, z, if x.equals (y) returns true, and Y.equals (z) returns ture, that
X.equals (z) must also return True.
4. consistency, for any non-null reference value x, y, as long as the information used in the two objects of the equals comparison operation has not been modified, multiple
Calling X.equals (y) returns True uniformly, or returns a consistent return of False.
5. For any non-null value x,x.equals (null), you must return False.

If these conventions are not fully adhered to in our program, then your program is likely to have problems.

There is no problem because your program has no place to call or indirectly invoke the equals method.
What is direct calling equals and what is indirect calling equals?
Calling the Equals method directly means that you explicitly invoke the Equals method in your program on the object you are writing to,
Indirect invocation, in a simple example, when we usually put a few objects into the collection, in order not to repeat, we have to call collection contains method, this time indirectly called the object in the collection equals Method.

In a class that overrides equals, you must also overwrite the Hashcode method

Simple Conclusion: when the object class does not conform to the canonical override equals () and hashcode () methods, two objects are compared

    1. If equals () is the same, then hashcode () must be the Same.
    2. If hashcode () is the same, then equals () is not necessarily the Same.

All week,String,Math, and Integer,Double .... When these wrapper classes override the equals () method in object, so that it no longer compares the handle (reference), it compares the value of the integer that is actually contained in the object, that is, the content is Compared.

The equals () method of object compares the address Value.
In general, if you want to put an object of a class into a container, you usually want to override itequals () method, which lets them compare address values instead of content Values. In particular, if you want to put the object of your class in a hash, you also need to rewriteHashcode () method, to be placed in an ordered container, and to override theCompareTo () Method.

Why to rewriteHashcode method?
We should know First.Java rules that determine whether two objects are equal. InIn the collection of java, the rule that determines whether two objects are equal is:

first, determine the two Object'sHashcode is equal
If not equal, neither object is considered equal
If they are equal, then the two objects are judged with aequals operation is equal
If not equal, neither object is considered equal 
If equal, consider two objects equal  

We in equals method needs downward transformation, the efficiency is very low, so first Judge The Hashcode method can improve Efficiency.

equals () equal two objects, hashcode () must be equal ,  
equals () is not equal to two objects, but does not prove their equals () methods are not equal to two objects, hashcode () are likely to be equal. (my understanding is that the hash code was generated when the conflict was created).  ,
anti-  come: hashcode (), will be able to launch equals () also unequal; hashcode () equal, equals () may be equal or unequal.

Share:

The hashcode () and equals () methods in the Java object class are reproduced-----

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.