This is the second day of my participation in Gwen Challenge
First, the equals() method is defined in the object class using the “==” method to match. The equals() method compares whether these references refer to the same object(==), if they are not overridden, and if they are not overridden in the parent list, then the equals() method compares whether these references refer to the same object(==). So == is comparing values; If it is a reference type, then the comparison is whether the two references refer to the same object.
The equals() method is there for you to override because you want subclasses to implement the ratio function. See String: Generally we compare strings to compare the contents of the two strings
Str1 = "666"; Str2 = new String (" 666 "); str1 == str2; // return false str1.equals(str2); // return trueCopy the code
Because String overrides equals() :
/** * Compares this string to the specified object. The result is {@code * true} if and only if the argument is not {@code null} and is a {@code * String} object that represents the same sequence of characters as this * object. * * @param anObject * The object to compare this {@code String} against * * @return {@code true} if the given object represents a {@code String} * equivalent to this string, {@code false} otherwise * * @see #compareTo(String) * @see #equalsIgnoreCase(String) */ public boolean equals(Object anObject) { if (this == anObject) { return true; } if (anObject instanceof String) { String anotherString = (String)anObject; int n = value.length; if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; while (n-- ! = 0) { if (v1[i] ! = v2[i]) return false; i++; } return true; } } return false; }Copy the code
Look at hashCode: This is a native method that returns the memory address of an object with the following constraints
1. The same integer must be consistently returned when the hashCode method is called on the same object multiple times during the execution of a Java application, provided that the information used to compare the objects to Equals has not been modified. The integer need not be consistent from one execution of an application to another execution of the same application. 2. If two objects are equal according to equals(Object), then calling the hashCode method on each of them must produce the same integer result. 3. Two objects do not have to produce different integer results if they are not equal according to equals(java.lang.object).
However, it is often possible to improve hash table performance by generating different integer results for unequal objects. The reality is that hashCode does not necessarily return the virtual memory address of the object, depending on the runtime library and JVM implementation. But it returns a unique hash value for different objects. So we have hashCode(), which determines the number of keys in the bucket, which is the index in the array; Equals () is used to compare whether two objects are the same.
So the summary is that if two objects are equal, hashcode has to be equal and two objects have to be equal