This tutorial explains the concept of the .equals method in Java. We will see how to use and override the equals method in Java with practical applications:
We will be learning the following concepts:
- What is .equals() method in Java?
- Difference between Java == operator and equals () method
- Java equals() in String class
- Java equals() in numeric data types
- Java equals() in Boolean data type
- Overriding equals () method in Java
Let’s understand what equals () method Java language provides.
=> Visit Here To Learn Java From Scratch
Table of Contents:
Java .equals() Method
Java equals () method is a method of the Java Object class. This object class is the root of the class hierarchy in Java. i.e. every class in Java has class Object as its superclass. Hence, all objects and arrays implement the methods of this object class.
Here is the method signature of the .equals Java method:
public boolean equals(Object ob)
This method returns if the specified other object is equal to this object. This equals method does this by implementing an equivalence relation on objects which are non-null reference values.
To override this method, it is usually necessary to override another method called hashCode. This is to fulfill the contract for the hashCode method as it says that objects which are equal must have equal hash codes.
Parameters:
ob: This is the reference object to which the current object needs to compare.
Returns:
true if the current object is the equal as the argument ob and returns false if objects are not equal.
Difference Between == Operator And Equals() Method
== is Java operator whereas equals() is the Java method. Java has provided equality and relational operators for comparison between two operands. ‘==’ is an Equality Operator provided in Java to compare if two operands are equal. “==”, “!=”, must be used for testing equality between 2 primitive values.
For Example, as seen in the below sample program, two integer operand values i.e. val1=7 and val2=-3 are compared first which returns false, and then compares val1=7 and val3=7 which returns true.
package com.softwaretestinghelp; public class EqualsDemo1 { public static void main(String[] args) { int val1 = 7; int val2 = -3; int val3 = 7; if(val1 == val2) { System.out.println("val1 and val2 are equal"); } else { System.out.println("val1 and val2 are Not equal"); } if(val3 == val1) { System.out.println("val1 and val3 are equal"); }else { System.out.println("val1 and val3 are not equal"); } } }
Here is the program Output:
val1 and val2 are Not equal
val1 and val3 are equal
Another difference between the == and equals Java method is ‘==’ operator compares references of Java objects and the ‘equals’ Java method compares the objects based upon the implementation of the method.
Let’s see the following sample program:
package com.softwaretestinghelp; public class EqualsDemo2 { public static void main(String[] args) { String s1 = new String("HELLO"); String s2 = new String("HELLO"); System.out.println("s1 and s2 are equal : "+(s1==s2)); System.out.println("s1 and s2 are equal : "+(s1.equals(s2))); s1 = s2; System.out.println("s1 and s2 are equal : "+(s1==s2)); } }
Here is the program Output:
s1 and s2 are equal : false
s1 and s2 are equal : true
s1 and s2 are equal : true
As seen in the above program, the two String variables are assigned values as follows:
String s1 = new String("HELLO"); String s2 = new String("HELLO");
When the s1 and s2 objects are compared with the ‘==’ operator:
System.out.println("s1 and s2 are equal : "+(s1==s2));
it gives the following output on the console:
s1 and s2 are equal : false
This result is returning ‘false’ because the ‘==’ operator is comparing references of objects s1 and s2 i.e. comparing addresses of objects. That is the reason the result is ‘false’ as s1 and s2 are different objects, so have different addresses.
s1 and s2 objects are compared with equals () method as follows:
System.out.println("s1 and s2 are equal : "+(s1.equals(s2)));
It gives the following output on the console:
s1 and s2 are equal : true
This is because the equals () method compares the content of objects s1 and s2. Both the String objects have the same content i.e. “HELLO”, which is why comparison returns a true value.
Now, with the following statement, we are assigning s2 to s1, this changes the reference address of s2 to the same as s1:
s1 = s2;
Hence, when we compare s1 with s2 with ‘==’ operator, it returns true as both are referring to the same object address i.e. have the same reference:
System.out.println("s1 and s2 are equal : "+(s1==s2));
This displays the following output on the console:
s1 and s2 are equal : true
Note: This equals method Java class has to override to have its own implementation for comparing two objects of that class. If this equals () method is not overridden, then by default equals (Object obj) method of the closest parent class which has overridden this method is used.
In this sample, we have used String objects for comparison and the String class has its own overridden implementation of the equals () method. Hence, the comparison is done by comparing the content of these objects.
Equals() Method In Java String Class
Let’s have look at how equals() work with the String class. We have already seen in the above sample program that when two String objects are compared using the equals () method, then it returns the result as true or false based on the content of the String objects. This is because the String class has overridden the equals () method.
Let’s see the method signature of the .equals Java method for the String class:
public boolean equals(Object ob)
This method does a comparison of this string with the specified object. This method returns true for the non-null argument which is a String object representing the same character sequence as the current object.
Parameters:
ob: This is the reference object to which the current String object needs to compare.
Returns:
This method returns true if the specified object ob represents a String which is equivalent to a current string, or else it returns false.
Let’s see the following sample program:
package com.softwaretestinghelp; public class EqualsDemo3 { public static void main(String[] args) { String sampleStr1 = new String("HELLO"); String sampleStr2 = new String("hello"); String sampleStr3 = null; String sampleStr4 = "HELLO"; System.out.println("sampleStr1 and sampleStr2 are equal : "+(sampleStr1.equals(sampleStr2))); System.out.println("sampleStr1 and sampleStr4 are equal : "+(sampleStr1.equals(sampleStr4))); System.out.println("sampleStr2 and sampleStr4 are equal : "+(sampleStr2.equals(sampleStr4))); System.out.println("sampleStr4 and sampleStr3 are equal : "+(sampleStr4.equals(sampleStr3))); } }
Here is the program Output:
sampleStr1 and sampleStr2 are equal : false
sampleStr1 and sampleStr4 are equal : true
sampleStr2 and sampleStr4 are equal : false
sampleStr4 and sampleStr3 are equal : false
In this program, we are assigning different values to String variables as follows:
String sampleStr1= new String("HELLO"); => new String object for uppercase HELLO String sampleStr2 = new String("hello");=> new String object for lower case hello String sampleStr3 = null; => null object String sampleStr4 = "HELLO"; => String literal HELLO
When we perform a comparison of 2 different String objects using the equals() method:
System.out.println("sampleStr1 and sampleStr2 are equal : "+(sampleStr1.equals(sampleStr2))); System.out.println("sampleStr1 and sampleStr4 are equal : "+(sampleStr1.equals(sampleStr4))); System.out.println("sampleStr2 and sampleStr4 are equal : "+(sampleStr2.equals(sampleStr4))); System.out.println("sampleStr4 and sampleStr3 are equal : "+(sampleStr4.equals(sampleStr3)));
We get different results as shown below:
- sampleStr1 and sampleStr2 are equal : false => This result is false as “HELLO” and “hello” are with a different case.
- sampleStr1 and sampleStr4 are equal : true => This result is true as both represent “HELLO” i.e. same character sequence and that is also in the same case i.e. Uppercase
- sampleStr2 and sampleStr4 are equal : false => This result is false as “HELLO” and “hello” are with different case i.e. uppercase and lowercase respectively.
- sampleStr4 and sampleStr3 are equal : false => This result is false as we are comparing with the null object s3.
Equals() Method In Java Numeric Data Types
Java supports primitive data types for numeric values like int, long, float, and double.
Java also provides a Wrapper class to create objects for representing these values like the Integer wrapper class to represent int values, Float wrapper class to represent values for float, etc. These classes also have their own implementation of the equals method.
Let’s have a look at the method signature for the .equals Java method for the Long class:
public boolean equals(Object ob)
This method does a comparison of this Long object with the specified object. This method returns true for the non-null argument which is a Long object representing the same long value as the current object.
Parameters:
ob: This is the reference object to which the current Long object needs to compare.
Returns:
This method returns true if the specified object ob represents a long value that is equivalent to the current object long value, or else it returns false.
Similarly, other Wrapper classes like Integer, Float, Double also have their own equals () method implementation. Let’s understand numeric values comparison using the equals () method with the help of the following program:
package com.softwaretestinghelp; public class EqualsDemo4 { public static void main(String[] args) { Long lng1 = 15l; Long lng2 = 15l; Double dbl1 = 15.5d; Double dbl2 = 20.0d; System.out.println("lng1 and lng2 are equal : "+(lng1.equals(lng2))); System.out.println("dbl1 and dbl2 are equal : "+(dbl1.equals(dbl2))); } }
Here is the program Output:
lng1 and lng2 are equal : true
dbl1 and dbl2 are equal : false
In the above program, we are comparing Two Long objects and Double objects as follows:
System.out.println("lng1 and lng2 are equal : "+(lng1.equals(lng2))); System.out.println("dbl1 and dbl2 are equal : "+(dbl1.equals(dbl2)));
This gives us the following output on the console:
- lng1 and lng2 are equal : true => This returns true as both the Long objects have the same values i.e.15l
- dbl1 and dbl2 are equal : false => This returns false as the Double objects have different floating number values i.e.15.5d and 20.0d
Equals() Method In Java Boolean Data Type
Java primitive data type boolean stores boolean values like true and false. Java provides a Wrapper class also i.e. Boolean class which represents boolean values like true or false. This class has its own implementation of the equals method.
Let’s have a look at the method signature for the .equals Java method of Boolean wrapper class:
public boolean equals(Object ob)
This method does a comparison of this Boolean object with the specified object. This method returns true for the non-null argument which is a Boolean object representing the same boolean value as the current object.
Parameters:
ob: This is the reference object to which the current Boolean object needs to compare.
Returns:
This method returns true if the specified object ob represents a boolean value that is equivalent to the current object boolean value, or else it returns false.
Let’s see the following sample program that shows a comparison of Boolean objects using equals method Java Boolean class:
package com.softwaretestinghelp; public class EqualsDemo5 { public static void main(String[] args) { Boolean bln1 = true; Boolean bln2 = new <del>Boolean</del>(true); Boolean bln3 = false; System.out.println("bln1 and bln2 are equal : "+(bln1.equals(bln2))); System.out.println("bln1 and bln3 are equal : "+(bln1.equals(bln3))); System.out.println("bln2 and bln3 are equal : "+(bln2.equals(bln3))); } }
Here is the program Output:
bln1 and bln2 are equal : true
bln1 and bln3 are equal : false
bln2 and bln3 are equal : false
In the above program, we are comparing Boolean objects using the equals () method as follows:
System.out.println("bln1 and bln2 are equal : "+(bln1.equals(bln2))); System.out.println("bln1 and bln3 are equal : "+(bln1.equals(bln3))); System.out.println("bln2 and bln3 are equal : "+(bln2.equals(bln3)));
This gives us different results as shown below:
- bln1 and bln2 are equal : true=> This returns true as Boolean objects both b1 and b2 have the same boolean value i.e. true
- bln1 and bln3 are equal : false=> This returns true as Boolean objects b1 and b3 have different boolean values i.e. true and false
- bln2 and bln3 are equal : false => This returns true as Boolean objects b1 and b3 have different boolean values i.e. true and false
Override Java Equals Method
As seen in the above examples, the .equals() method can be overridden in the class. But the classes we have seen in the above samples are Java classes like String, Boolean, and Long, etc.
This method can be overridden in custom classes as well by having its own implementation. Let us understand this equals method overriding with the following sample program.
Here we are defining our own Java class called EqualsSample:
package com.softwaretestinghelp; public class EqualsDemoSample { private String objName = null; public EqualsDemoSample(String name){ this.objName= name; } public void setName(String name) { this.objName = name; } public String getName() { return this.objName; } @Override public boolean equals(Object obj) { if(obj instanceof EqualsDemoSample) { EqualsDemoSample equalsSample = (EqualsDemoSample) obj; if(equalsSample.getName().equals(this.getName())){ return true; } } return false; } }
In the above class, we have defined our own overridden equals () method that compares the ‘name’ property value of the class instance as follows:
if(obj instanceof EqualsDemoSample) { EqualsDemoSample equalsSample = (EqualsDemoSample) obj; if(equalsSample.getName().equals(this.getName())){ return true; } }
Let us see how to compare the two objects of EqualsSample class with the help of the following program:
package com.softwaretestinghelp; public class EqualsDemo6 { public static void main(String[] args) { EqualsDemoSample eqlDemoSmpl1 = new EqualsDemoSample("Car"); EqualsDemoSample eqlDemoSmpl2 = new EqualsDemoSample("Bicycle"); EqualsDemoSample eqlDemoSmpl3 = new EqualsDemoSample("Car"); System.out.println("eqlDemoSmpl1 and eqlDemoSmpl2 are equal : " +(eqlDemoSmpl1.equals(eqlDemoSmpl2))); System.out.println("eqlDemoSmpl2 and eqlDemoSmpl3 are equal : " +(eqlDemoSmpl2.equals(eqlDemoSmpl3))); System.out.println("eqlDemoSmpl1 and eqlDemoSmpl3 are equal : " +(eqlDemoSmpl1.equals(eqlDemoSmpl3))); } }
Here is the program Output:
eqlDemoSmpl1 and eqlDemoSmpl2 are equal : false
eqlDemoSmpl2 and eqlDemoSmpl3 are equal : false
eqlDemoSmpl1 and eqlDemoSmpl3 are equal : true
In the above program, we are comparing 2 EqualsSample objects as follows:
System.out.println("eqlDemoSmpl1 and eqlDemoSmpl2 are equal : " +(eqlDemoSmpl1.equals(eqlDemoSmpl2))); System.out.println("eqlDemoSmpl2 and eqlDemoSmpl3 are equal : " +(eqlDemoSmpl2.equals(eqlDemoSmpl3))); System.out.println("eqlDemoSmpl1 and eqlDemoSmpl3 are equal : " +(eqlDemoSmpl1.equals(eqlDemoSmpl3)));
This gives the following output on the console:
- eqlDemoSmpl1 and eqlDemoSmpl2 are equal : false=> This returns false as the name values of equalsSample1 and equalsSample2 are different i.e. “Car” and “Bicycle”
- eqlDemoSmpl2 and eqlDemoSmpl3 are equal : false=> This returns false as the name values of equalsSample2 and equalsSample3 are different i.e. “Bicycle” and “Car”
- eqlDemoSmpl1 and eqlDemoSmpl3 are equal : true=> This returns false as the name values of equalsSample1 and equalsSample3 are different i.e. “Car” and “Car”
With this, we have seen the use of the equals() method with different Java types. Now, let’s have a look at the frequently asked questions about the equals () method.
Frequently Asked Questions
Q #1) What is the equals method in Java?
Answer: In Java, the ‘equals()’ method is a method of the Java Object class. This object class is the root of the class in Java. Hence, all Java classes by default inherit the equals () method from this root class.
Q #2) What is the difference between Java == and equals () method in Java?
Answer: == is a Java operator that is used for primitive data type values comparison and equals () is a method in Java that compares values in Java objects or Java object references.
Q #3) What is == in Java?
Answer: == is the Equality operator provided by Java. This is used for comparing values for primitive Java operands and reference values of Java objects.
Q #4) Can we override the equals method in Java?
Answer: We can override the equals() method in the class. This is done by having its own implementation in overridden equals() method of the class. This is used for comparing two objects of the same class.
Conclusion
In this tutorial, we have seen the following concepts along with the use of the Java equals method in the Java class:
- What is .equals() method in Java?
- Difference between Java == operator and ,equals() method
- Java equals() in String class
- equals() method in numeric data types
- .equals() method in Boolean data type
- Overriding equals() method in Java
We covered each of the above concepts in detail and explained the use of the equals() method with the help of sample Java programs.
=> Explore The Simple Java Training Series Here