Java Basics Getting Started-hashcode and Equils methods

Source: Internet
Author: User

Features of the Equils method:

1. reflexivity, for any non-null reference x,x.equils (x) returns True

2. Symmetry, for any reference x,y,x.equils (Y) returns True, then Y.equils (x) returns True

3. transitivity, for any application of x, Y, Z, if X.equils (y) returns True, and X.equils (z) returns True, then Y.equils (z) returns True

4. Consistency, if x, Y, no change, then X.equils (y) should return the agreed result

5. Returns false for any non-null x,x.equils (NULL)

Hashcode, a hash code, is an integer value exported by an object.

However, for different classes, it is possible for him to rewrite the equils and Hashcode methods

We need to pay attention to these two places when we compare two objects in a normal time.

The following is a detailed code example

Package com.ray.testobject;/** * Equils and Hashcode * * @author Ray * @since 2015-05-02 * @version 1.0 */public class Test { public static void Main (string[] args) {String a = "OK"; String b = new string ("OK"); System.out.println (A.equals (b));//The following is the Equils method that is overridden in a string//You can see the first comparison of two obj is consistent, obviously not the same string//and then it inside the string inside again to compare each character, String consistent/public boolean equals (Object anobject) {///if (this = AnObject) {//return true;//}//if (anobject ins) as long as the character is determined to be identical Tanceof string) {//String anotherstring = (string) anobject;//int n = count;//if (n = = Anotherstring.count) {//char v1[ ] = value;//char v2[] = anotherstring.value;//int i = offset;//int j = anotherstring.offset;//while (n--! = 0) {//IF (v1[i++]! = v2[j++])//return false;//}//return true;//}//}stringbuilder ASB = new StringBuilder (a); StringBuilder BSB = new StringBuilder (b); System.out.println (Asb.equals (BSB));//Because StringBuilder does not override the Equils method, it is called the Equils method inside the object//The following is the method inside the object, He has to compare whether the object pointed to is consistent, so the return is false//public boolean equals (object obj) {//return (this = = obj);//}//Similarly, string has been rewritten in the Hashcode method, and StringBuilder does not have System.out.println ("A.hashcode:" + A.hashcode ()); System.out.println ("B.hashcode:" + b.hashcode ()); System.out.println ("Asb.hashcode:" + asb.hashcode ()); System.out.println ("Bsb.hashcode:" + bsb.hashcode ());}}


Output:

True
False
a.hashcode:3548
b.hashcode:3548
asb.hashcode:29013258
bsb.hashcode:12830537

As can be seen from the above code, each class may override the corresponding Equils and Hashcode methods, so

You must pay attention to the use of the above two methods when comparing objects.

Java Basics Getting Started-hashcode and Equils methods

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.