Java Object equals() Method
The Object equals() method is used to compare whether two objects are equal.
The equals() method compares two objects and determines whether the two object references point to the same object, i.e., it only checks whether the two objects point to the same address in memory.
Note:If a subclass overrides the equals() method, it needs to overridethe hashCode() method, for example, the String class overrides the equals() method and also overrides the hashCode() method.
Syntax
object.equals(Object obj)
Parameter
obj- The object to be compared.
Return Value
Returns true if the two objects are equal, otherwise returns false.
Example
The following example demonstrates the use of the equals() method:
Example
The above program produces the following result:
false true
The String class overrides the equals() method to compare whether two strings are equal:
Example
The above program produces the following result:
true false
Other Extensions
Java Object Class