Let’s learn difference between == operator and .equals() method in java.
Difference between == operator and .equals() method in java
In general == is an operator and .equals() is a method. We use == operator for reference comparison of objects in heap. Because in java we don’t have concept of operator overloading.
== operator is used for checking address of two objects is same or not. That is, == operator checks if both objects point to the same memory location or not.
But .equals() method of String class is meant for content comparison. Here in .equals() method the objects can be same or different content should be same.
Now let’s learn the difference between .equals() method and == operator with the help of an example.
In the below example we have created two strings str1 and str2. Now let’s compare these two strings using == operator and .equals() method to check whether they are equal or not.
First using == operator. For == comparison output will be false. Because “str1” and “str2” have different addresses. == operator is used for reference comparison.
for better understanding learn string constant pool.
Now using .equals() method; Here the output will be true. Because .equals() method of String class overrides equals() method of Object class.
Let’s see the difference between == and equals method in java with example.
Example:
// == operator String str1 = new String("Hello"); String str2 = new String("Hello"); System.out.println(str1 == str2); // output : false // .equals() method String str1 = new String("Hello"); String str2 = new String("Hello"); System.out.println(str1.equals(str2)); // output : true
NOTE:
- For any object reference “obj”, it may be String, Thread; obj == null is always “false”. If we comapre object reference with “null” the result is always “false”. But null == null is always “true”.
- While equals() method of String class (which overrides equals() method of Object class) is used to compare contents.
== and equals() example
public class OperatorMethodExample { public static void main(String[] args) { String str1 = new String("helloworld"); String str2 = new String("helloworld"); // two strings have different object so output is false boolean output = str1 == str2; System.out.println("Comparing with == operator : " + output); // strings contain same content hence true // java.lang.String class override equals method of Object class output = str1.equals(str2); System.out.println("Comparing with same content using .equals() method : " + output); str2 = str1; // both str1 and str2 reference are pointing to same object hence // "==" operator return true output = (str1 == str2); System.out.println("Comparing with == operator : " + output); } }
Output:
Comparing with == operator : false
Comparing with same content using .equals() method : true
Comparing with == operator : true
Now let’s compare two objects using == operator and equals() method.
public class ComparisonDemo { public static void main(String[] args) { Object ob1 = new Object(); Object ob2 = new Object(); boolean output; output = (ob1 == ob2); System.out.println("== operator : " + output); output = ob1.equals(ob2); System.out.println("equals() method : " + output); ob1 = ob2; output = (ob1 == ob2); System.out.println("Comparing two reference pointing to same Object using == operator : " + output); } }
Output:
== operator : false
equals() method : false
Comparing two reference pointing to same Object using == operator : true
How equals method works in java – java equals override
equals() method of Object class is used to compare address of two objects.
class Object { public boolean equals(Object obj) { return (this == obj); // this refers to current class instance variable } }
equals() method of String class is used to compare content of two objects. Here equals() method of Object is overridden by equals() method of String class.
class String extends Object { @Override public boolean equals(Object str) { // content comparison } }