Junior — Middle
Is it possible to use the equals method to check if two different objects are equal in content?
sobes.tech AI
Answer from AI
Yes, in Java, the equals method is used to check for object equality based on content, not reference. By default, the equals method in the Object class compares references, but many classes (such as String, Integer) override this method to compare content.
Example:
String a = new String("test");
String b = new String("test");
System.out.println(a == b); // false, different objects
System.out.println(a.equals(b)); // true, same content
Important: for equals to work correctly, the class must properly override this method, considering all significant fields.