[Android] Watch blog Learning hashcode () and Equals ()

Source: Internet
Author: User

Equals () is A method provided by the object class, and it is well known that each Java class inherits from Object, So every object has a equals () method, which we generally override when we use this method.

The equals () method in the Object class source code

     Public Boolean equals (Object obj) {        return ( this = = obj);    }

At this point the comparison is whether two references point to the same object, which is often used when we want to compare the logical equality of two objects

the String class overrides the equals () method, so:

        String a=New string ("AA");        String b= "AA";        System.out.println (A.equals (b)); // true        System.out.println (a==b);        // false                 System.out.println (A.hashcode ());        System.out.println (B.hashcode ());

The Hashcode () method is also inherited from the object class, which returns the hash code value of an object, which is usually the integer returned by the internal address of the objects, and is implemented primarily to improve the performance of the hash table.

The return value of Hashcode () is related to equals () as follows

If x.equals (y) returns true , then the x and y hashcode () values must be equal

If x.equals (y) returns false , then the x and Y Hashcode () values may be equal and may vary

Overriding the equals () method will override the hashcode () method

[Android] Watch blog Learning hashcode () and Equals ()

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.