Introduction
In Java, the equals() & hashCode() methods are essential concepts that every developer should understand. These methods are used to compare objects & determine their equality. The equals() method is used to compare two objects for equality, while the hashCode() method returns a unique integer value for each object.

In this article, we will learn the basics of equals() & hashCode(), their syntax, and principles, with examples.
equals() method
The equals() method is a fundamental method in Java that is used to compare two objects for equality. It is defined in the Object class, which is the parent class of all classes in Java. The equals() method takes an object as a parameter & returns a boolean value indicating whether the two objects are equal or not.
The default implementation of the equals() method in the Object class compares the memory addresses of the two objects. However, this behavior can be overridden in custom classes to provide a more meaningful comparison based on the objects' properties or attributes.
Syntax
The syntax for the equals() method is as follows:
public boolean equals(Object obj) {
// comparison logic here
}
To override the equals() method in your custom class, you need to follow these rules:
1. The method must be declared as public.
2. The method must take an Object as a parameter.
3. The method must return a boolean value.
Inside the method, you write the comparison logic to determine whether the two objects are equal based on their relevant properties or attributes.
Some principles of equals() method of Object class:
When overriding the equals() method in your custom class, there are some important principles to keep in mind:
1. Reflexive: For any non-null reference value x, x.equals(x) should return true.
2. Symmetric: For any non-null reference values x & y, x.equals(y) should return true if & only if y.equals(x) returns true.
3. Transitive: For any non-null reference values x, y, & z, if x.equals(y) returns true & y.equals(z) returns true, then x.equals(z) should return true.
4. Consistent: Multiple invocations of x.equals(y) should consistently return true or false, provided no information used in the comparison is modified.
5. For any non-null reference value x, x.equals(null) should return false.
Program
Let's take a look at an example program that shows the usage of the equals() method:
Output
True
False
In this example, we have a `Person` class with `name` & `age` attributes. We override the `equals()` method to compare two `Person` objects based on their `name` & `age`. The `equals()` method first checks if the two objects are the same instance, then it checks if the other object is null or of a different class. Finally, it compares the `name` & `age` attributes of the two objects.
In the `Main` class, we create three `Person` objects & compare them using the `equals()` method. The output will be `true` for `person1.equals(person2)` since they have the same `name` & `age`, & `false` for `person1.equals(person3)` since they have different `name` & `age`.