"= =" 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