String and Equals (), Hashcode ()

Source: Internet
Author: User
"= =" in object, Equal,hashcode () = = In the objectFor the base data type, the "= =" Comparison value is the same. For reference data types, "= =" compares the stored addresses in memory for the same. equals in Object ()
public boolean equals (Object o) {return this
    = = O;
}

The Equals () method of object is either by default or by "= =" compared, so the memory address is compared. hashcode in Object ()

public int hashcode () {
    int lockword = shadow$_monitor_;
    Final int lockwordstatemask = 0xc0000000;  Top 2 bits.
    Final int lockwordstatehash = 0x80000000;  Top 2 bits are value 2 (kstatehash).
    Final int lockwordhashmask = 0X0FFFFFFF;  Low bits.
    if ((Lockword & lockwordstatemask) = = Lockwordstatehash) {return
        Lockword & lockwordhashmask;
    }
    Return System.identityhashcode (this);
}

The Hashcode () return value in object is the 32-bit address in the JVM. String Class "= =", Equal,hashcode ()

Hashcode () and Equals () are overridden in the

String class, so the Equals () and hashcode () of the call string may produce different results than classes that do not override the two methods. string equals ()

The same is true if the memory address is the same and the content is the same.
The storage address is different to satisfy the length, the hash code, the corresponding characters are the same to return true. hashcode of String ()

@Override public int hashcode () {
    int hash = hashcode;
    if (hash = = 0) {
        if (count = = 0) {return
            0;
        }
        for (int i = 0; i < count; ++i) {
            hash = * Hash + charAt (i);
        }
        Hashcode = hash;
    }
    return hash;
}

Generates hashcode based on the string's member variable hashcode and the contents of the string. Summary:

By default, = = compares the storage address of the object, hashcode the object to which it is returned.
Equal compares objects by default and compares the addresses of objects in the JVM, with = =
Most classes rewrite equals () according to their capabilities, but the general overriding equals () overrides Hashcode () to redefine the hash code specification.

More detailed distinctions and general conventions see the difference between equals and equals in Java hashcode

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.