Both the equals() method and the == operator are used to compare two objects in Java.
The Java string equals() method, compares two strings and returns true if all characters match in both strings, else returns false. The String equals() method overrides the equals() method of the Object class.
The == operator compares the reference or memory location of objects in a heap, whether they point to the same location or not.
Whenever we create an object using the operator new, it will create a new memory location for that object. So we use the == operator to check memory location or address of two objects are the same or not.
The Java String class equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.
Comments