In this article we will continue to explain the remaining 10 methods of Object class in java.lang package.
equals(Object o)
Prototype of this method is
public boolean equals(Object o)
We use Equals() method in order to check the equality of any 2 objects. Method equals() is meant for reference comparison or address comparison, and when address or reference of 2 objects are equal then only it return true. For example:
public class Mall { String store_name; int store_id; Mall(String store_name, int store_id) { this.store_name = store_name; this.store_id = store_id; } public static void main(String []args) { Mall m1 = new Mall("Levi's",1001); Mall m2 = new Mall("Mcdonalds",1002); Mall m3 = new Mall("Levi's",1001); Mall m4 = m1; System.out.println(m1.equals(m2)); //comparing m1 and m2 System.out.println(m1.equals(m3)); //comparing m1 and m3 System.out.println(m2.equals(m3)); //comparing m1 and m3 System.out.println(m1.equals(m4)); //comparing m1 and m4 } }
Output of the above code will be
false false false true
Note- In the above code, as you can see that we pass the object m1 and m3 with the same values. But still when we pass them to equals() it is giving false. This is because addresses of object of m1 and m3 are not same. But in the case of m1 and m4, m4 is given the reference of m1 and therefore they are pointing to the same object. Therefore in the 4th case true is the output of equals.
If we want to compare content of reference rather than addresses then we need to override the equals() method in our program. Through this, equals() will show the exact same behavior as “==” operator.
While overriding equals(), some key points should be considered. These key points are:
- What is the meaning of equality?
- In the case of different types of objects or heterogeneous objects, equals() should returnĀ false rather than throwing ClassCastException.
- In case if we are passing null arguments, equals() should returnĀ false rather than throwing NullPointerException.
Valid way of overriding equals() in our own class is as follows:
public class EmployeeManager { //initializing variables String name; int empid; //defining constructor EmployeeManager(String name, int empid) { name = this.name; empid = this.empid; } //overriding equals() public boolean equals() { try { EmployeeManager e = (EmployeeManager)o; if(name.equals(e.name) && empid.equals(e.empid)) { return true; } else { return false; } } catch(ClassCastException cce) { return false; } catch(NullPointerException cce) { return false; } } public static void main(String []args) { EmployeeManager e1 = new EmployeeManager("james",10001); EmployeeManager e2 = new EmployeeManager("bucky",10002); EmployeeManager e3 = new EmployeeManager("james",10001); EmployeeManager e4 = e1; //comparing e1 and e2 System.out.println(e1.equals(e2)); //comparing e1 and e3 System.out.println(e1.equals(e3)); //comparing e1 and e4 System.out.println(e1.equals(e4)); //comparing e1 and string System.out.println(e1.equals("james")); //comparing e1 and null System.out.println(e1.equals(null)); } }
Output of the above code is:
false true true false false
Relationship between equals() method and “==” operator
Following are some of the relationships:
- If r1==r2 is true then r1.equals(r2) is also true.
- If r1==r2 is false then r1.equals(r2) may be true or may be false.
- If r1.equals(r2) is true then r1==r2 may be true or may be false.
- If r1.equals(r2) is false then r1==r2 is always false.
Relationship between equals() hashCode()
- If 2 objects are equal as given by equals() then their hashcodes will also be same.
- If 2 objects are not equal as given by equals() then their hashcodes might be same or might not be same.
- If hashcodes of 2 objects are same then on using equals() they might return true or might return false.
- If hashcodes of 2 objects are different then on using equals() they will definitely be false.