equals and hashCode are defined in the Object class, which is the parent object of all Java objects
-> Therefore, all Java objects inherit the equals and hashCode functions defined in the Object class
- When the operands are
primitive types(int, byte, short, long, float, double, boolean, char), itcompares values, - When the operands are other
reference types, itcompares the addresses they point to
public boolean equals(Object obj) {
return (this == obj);
}- Used to check whether 2 objects are identical
- It checks whether the 2 objects refer to the same thing
- That is, 2 objects are identical only when they point to the
same memory address
- That is, 2 objects are identical only when they point to the
- When we create 2 identical strings, the 2 strings are allocated in different memory locations
- However, the reason equals returns true is that the String class
overridesthe equals method to return true when the string contents are the same- It compares each character one by one and returns true if they are all identical
- Therefore, even different objects are judged to be identical if they have the same string
- However, the reason equals returns true is that the String class
public native int hashCode();- It refers to a unique integer value that can identify an object
- In the Object class, it is set to return the
memory address of the object stored in heap memory
- In the Object class, it is set to return the
- The
nativekeyword means the method is implemented using native code calledJNI (Java Native Interface)- native is a keyword applicable only to methods, used when utilizing parts implemented in languages other than Java through JNI in Java
- It is a keyword that enables using other languages from Java!
- native is a keyword applicable only to methods, used when utilizing parts implemented in languages other than Java through JNI in Java
- hashCode is
used to determine the location where data is storedwhen using data structures such asHashTable