eqlDemoSmpl1 and eqlDemoSmpl3 are equal : true=> This returns false as the name values of equalsSample1 and equalsSample3 are different i.e. βCarβ and βCarβ. With this, we have seen the use of the equals () method with different Java types. Now, letβs have a look at the frequently asked questions about the equals () method.
Since HashMap and Hashtable in Java rely on the equals () and hashCode () method for comparing keys and values, Java provides the following rules to override the equals method Java. 1) Reflexive: Object must be equal to itself. 2) Symmetric : if a.equals (b) is true then b.equals (a) must be true.Check the contract of equals method: it must be consistent (if the objects are not modified, then it must keep returning the same value). That's why StringBuilder does not override it regardless of its content. Let's take example above. StringBuilder s1 = new StringBuilder("Test"); StringBuilder s2 = new StringBuilder("Test");
Java Object hashCode () is a native method and returns the integer hash code value of the object. The general contract of hashCode () method is: Multiple invocations of hashCode () should return the same integer value, unless the object property is modified that is being used in the equals () method. An object hash code value can change in