equals() method is a non-static method of java.lang.Object class. As all classes in java are sub classes of Object class, this method is inherited to every class you create in java. Here is the method signature of equals() method.
public boolean equals(Object obj)
equals() method takes one argument “obj” of type Object class and returns true if object calling this method is same as “obj”. For example, if a1 and a2 are two reference variables of type class A and they are pointing to same object then invoking a1.equals(a2) will return true.
Below is the example to demonstrate the equals() method.
class A
{
int i = 10;
}
public class Test
{
public static void main(String[] args)
{
//Creating object of Class A and assigning it to a1
A a1 = new A();
//a1 is assigned to a2
A a2 = a1;
//That means a1 and a2 are pointing to same object.
//Calling a1.equals(a2) will return true
System.out.println(a1.equals(a2)); //Output : true
//Creating another object of Class A and assigning it to a2
a2 = new A();
//Now, a1 and a2 will be pointing to different objects.
//Now calling a1.equals(a2) will return false
System.out.println(a1.equals(a2)); //Output : false
}
}
Overriding equals() method in your class :
As equals() method is not a final method, you can override it in your class so that you can compare the contents of two objects not just their physical address. i.e you can override equals() method so that it should return true if two objects have same contents.
Below example shows how to override equals() method to compare the contents of two objects.
class A
{
int i;
public A(int i)
{
this.i = i;
}
//Overriding equals() method
@Override
public boolean equals(Object obj)
{
A a = (A) obj;
//Comparing the fields of calling object and passed object
return this.i == a.i;
}
}
public class Test
{
public static void main(String[] args)
{
//Creating object of Class A with i = 10
A a1 = new A(10);
//Creating object of Class A with i = 20
A a2 = new A(20);
//a1 and a2 have different contents
//So calling a1.equals(a2) will return false
System.out.println(a1.equals(a2)); //Output : false
//Changing the value of a2.i to 10
a2.i = 10;
//Now, a1 and a2 have same contents
//Now, calling a1.equals(a2) will return true
System.out.println(a1.equals(a2)); //Output : true
}
}
Difference Between “==” Operator and “equals() method” :
As both “==” operator and equals() method are used to compare two objects, here are some differences between them.
“==” operator returns true if and only if two references variables are pointing to same object. That means it checks only physical address of the objects not their contents. Default version of equals() method also compares physical address of two objects, but you can override and modify it to compare the contents of two objects.
You can use “==” operator to compare derived types as well as primitive types. But, equals() method is used only to compare the derived types.
Some Extra Points About equals() method :
public class Test
{
public static void main(String[] args)
{
Integer I1 = new Integer(10);
Integer I2 = new Integer(10);
System.out.println(I1.equals(I2)); //Output : true
Double D1 = new Double(25.6);
Double D2 = new Double(25.6);
System.out.println(D1.equals(D2)); //Output : true
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1.equals(s2)); //Output : true
}
}
Here is an example to show how to override equals() method to compare two objects having more than one fields.
class A
{
int i;
double j;
String s;
public A(int i, double j, String s)
{
this.i = i;
this.j = j;
this.s = s;
}
//Overriding equals() method
@Override
public boolean equals(Object obj)
{
A a = (A) obj;
//Comparing the fields of calling object and passed object
return this.i == a.i && this.j == a.j && this.s == a.s;
}
}
public class Test
{
public static void main(String[] args)
{
A a1 = new A(10, 25.6, "First_Object");
A a2 = new A(20, 45.8, "Second_Object");
System.out.println(a1.equals(a2)); //Output : false
a2 = new A(10, 25.6, "First_Object");
System.out.println(a1.equals(a2)); //Output : true
}
}
public boolean equals(Object obj)
equals() method takes one argument “obj” of type Object class and returns true if object calling this method is same as “obj”. For example, if a1 and a2 are two reference variables of type class A and they are pointing to same object then invoking a1.equals(a2) will return true.
Below is the example to demonstrate the equals() method.
class A
{
int i = 10;
}
public class Test
{
public static void main(String[] args)
{
//Creating object of Class A and assigning it to a1
A a1 = new A();
//a1 is assigned to a2
A a2 = a1;
//That means a1 and a2 are pointing to same object.
//Calling a1.equals(a2) will return true
System.out.println(a1.equals(a2)); //Output : true
//Creating another object of Class A and assigning it to a2
a2 = new A();
//Now, a1 and a2 will be pointing to different objects.
//Now calling a1.equals(a2) will return false
System.out.println(a1.equals(a2)); //Output : false
}
}
Overriding equals() method in your class :
As equals() method is not a final method, you can override it in your class so that you can compare the contents of two objects not just their physical address. i.e you can override equals() method so that it should return true if two objects have same contents.
Below example shows how to override equals() method to compare the contents of two objects.
class A
{
int i;
public A(int i)
{
this.i = i;
}
//Overriding equals() method
@Override
public boolean equals(Object obj)
{
A a = (A) obj;
//Comparing the fields of calling object and passed object
return this.i == a.i;
}
}
public class Test
{
public static void main(String[] args)
{
//Creating object of Class A with i = 10
A a1 = new A(10);
//Creating object of Class A with i = 20
A a2 = new A(20);
//a1 and a2 have different contents
//So calling a1.equals(a2) will return false
System.out.println(a1.equals(a2)); //Output : false
//Changing the value of a2.i to 10
a2.i = 10;
//Now, a1 and a2 have same contents
//Now, calling a1.equals(a2) will return true
System.out.println(a1.equals(a2)); //Output : true
}
}
Difference Between “==” Operator and “equals() method” :
As both “==” operator and equals() method are used to compare two objects, here are some differences between them.
“==” operator returns true if and only if two references variables are pointing to same object. That means it checks only physical address of the objects not their contents. Default version of equals() method also compares physical address of two objects, but you can override and modify it to compare the contents of two objects.
You can use “==” operator to compare derived types as well as primitive types. But, equals() method is used only to compare the derived types.
Some Extra Points About equals() method :
- equals() method is reflexive. i.e for any non-null reference variable ‘a’, a.equals(a) will return true.
- equals() method is symmetric. i.e for any non-null reference variables ‘a’ and ‘b’, a.equals(b) will return true if and only if b.equals(a) returns true.
- equals() method is transitive. i.e for any non-null reference variables ‘a’, ‘b’ and ‘c’, if a.equals(b) returns true and b.equals(c) returns true then a.equals(c) will also return true.
- equals() method is consistent. i.e for any non-null reference variables ‘a’ and ‘b’, multiple invocations of a.equals(b) will return either consistently true or consistently false, provided information used in equals() method on objects is not changed.
- In all wrapper classes and String class, equals() method is overrided to compare the two objects on contents. i.e if there are two Integer objects or two String objects or two Double objects with same value then comparing them through equals() method will return true.
public class Test
{
public static void main(String[] args)
{
Integer I1 = new Integer(10);
Integer I2 = new Integer(10);
System.out.println(I1.equals(I2)); //Output : true
Double D1 = new Double(25.6);
Double D2 = new Double(25.6);
System.out.println(D1.equals(D2)); //Output : true
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1.equals(s2)); //Output : true
}
}
Here is an example to show how to override equals() method to compare two objects having more than one fields.
class A
{
int i;
double j;
String s;
public A(int i, double j, String s)
{
this.i = i;
this.j = j;
this.s = s;
}
//Overriding equals() method
@Override
public boolean equals(Object obj)
{
A a = (A) obj;
//Comparing the fields of calling object and passed object
return this.i == a.i && this.j == a.j && this.s == a.s;
}
}
public class Test
{
public static void main(String[] args)
{
A a1 = new A(10, 25.6, "First_Object");
A a2 = new A(20, 45.8, "Second_Object");
System.out.println(a1.equals(a2)); //Output : false
a2 = new A(10, 25.6, "First_Object");
System.out.println(a1.equals(a2)); //Output : true
}
}