YouTip LogoYouTip

Java Object Equals

# Java Object equals() Method \\n\\n[![Image 3: Java StringClass](#)Java Object Class](#)\\n\\n* * *\\n\\nThe Object equals() method is used to compare whether two objects are equal.\\n\\nThe equals() method compares two objects to determine if two object references point to the same object, i.e., it only checks whether two objects point to the same address in memory.\\n\\n**Note:** If a subclass overrides the equals() method, it needs to override the [hashCode() method](#). For example, the String class overrides the equals() method and also overrides the hashCode() method.\\n\\n### Syntax\\n\\nobject.equals(Object obj)\\n### Parameters\\n\\n* **obj** - The object to compare.\\n\\n### Return Value\\n\\nReturns true if the two objects are equal, otherwise returns false.\\n\\n### Instance\\n\\nThe following instance demonstrates the use of the equals() method:\\n\\n## Instance\\n\\n```class TutorialTest\\n{\\n public static void main(String[] args)\\n {\\n // Object ClassUse the equals() Method\\n // Create two objects\\n Object obj1 = new Object();\\n Object obj2 = new Object();\\n\\n // Check if obj1 and obj2 are equal\\n // Different objects, different memory addresses, not equal, returns false\\n System.out.println(obj1.equals(obj2)); // false\\n\\n // obj1 Assign to obj3\\n // String Overridden equals() Method\\n // Object references, same memory address, equal, returns true\\n Object obj3 = obj1;\\n System.out.println(obj1.equals(obj3)); // true\\n }\\n}\\n\\nThe output of the above program is:\\n\\nfalse\\ntrue\\n\\nThe String class overrides the equals() method to compare the contents of two String objects.
← Java Object GetclassJava Object Class β†’