In this article, we will discuss the difference between equals() method and “==” operator.
So what would be my answer if anyone asks me this question? My answer would be, there is no difference in == and equals. I know this answer is not the same as you have heard till now. Don’t worry I will prove my point.
To understand the difference, You need to understand the question carefully. If I ask the difference between == and equals I am asking this regarding Objects class. But most of the time people relate this question with String class only and answer accordingly.
Before you go into details first let me tell you where this method is coming from. Object class has nine methods.
- clone()
- wait()
- wait(long)
- wait(long, int)
- notify()
- notifyAll()
- toString()
- equals()
- hashcode()
All classes in the java world inherit these methods. As you can see equals() method is also the part of Object class, hence it will be available to all java classes.
Syntax of equals() method in Object class
public boolean equals(Object obj) {
return (this == obj);
}
As you can see equals method in Object class only uses “==” operator. So there will not be any difference between “==” operator and equals() method if the class is not overriding equals method. Object’s implementation of equals is the same as ==.
Code sample for Class(equals method Not Overridden)
public class JBT_Equals {
public static void main(String[] args) {
JBT obj1 = new JBT("JBT");
JBT obj2 = new JBT("JBT");
System.out.println("References are pointing to same object ? :"+(obj1==obj2));
System.out.println("Objects are equals ? :"+obj1.equals(obj2));
}
}
/*
* In this class we have not overriden equals method hence equals method will
* behave in the same way as == operator
*/
class JBT {
String str;
JBT(String string) {
str = string;
}
}
Output
References are pointing to same object ? :false
Objects are equals ? :false
Code sample for Class (equals method overridden)
Note here the difference between the output from the above code.
public class JBT_Equals {
public static void main(String[] args) {
JBT obj1 = new JBT("JBT");
JBT obj2 = new JBT("JBT");
System.out.println("References are pointing to same object ? :"
+ (obj1 == obj2));
System.out.println("Objects are equals ? :" + obj1.equals(obj2));
}
}
/*
* In this class we have not overriden equals method hence equals method will
* behave in the same way as == operator
*/
class JBT {
String str;
JBT(String string) {
str = string;
}
/*
*
* @see java.lang.Object#equals(java.lang.Object)
*
* Here we have overridden the equals method from Object class. We are
* checking if string value in object is same then Objects are equals
* otherwise not.
*/
@Override
public boolean equals(Object obj) {
if (obj instanceof JBT) {
if (this.str.equals(((JBT) obj).str))
return true;
else
return false;
}
return false;
}
}
And now output would be different.
References are pointing to same object ? :false
Objects are equals ? :true
String class Behaviour
Whenever someone asks about the difference between equals and “==” people used to say the equals() method compare the value while “==” compare the reference of Object. This is true only(Other classes inherit the default behavior as mentioned above) in the case of String. Because String class has an overridden version of equals() method.
Syntax of equals() method in String Class
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
As you can see here the exact value is compared instead of reference.
Code sample for String Class(equals method Overridden)
/*
* Here we will learn about the String version of equals
* and == operator
*/
public class JBT_String {
public static void main(String[] args) {
String string ="JBT";
String compareString ="JBT";
// Here we will check if two reference points to the same object or not?
System.out.println("Reference pointing to same Object :"+(string==compareString));
//Here we will use to check if two string has the same VALUE or not?
System.out.println("Two string has same value? : "+string.equals(compareString));
}
}
Output
Reference pointing to same Object :true
Two string has same value? : true
Some Question for you
Please tell me why this code is giving this output.
Modified Code
public class EqualExample {
public static void main(String[] args) {
String str = "Hello";
String str1 = "Hello";
System.out.println(str == str1);
System.out.println(str1.equals(str));
}
}
The output of the above code is
true
true
So now tell me what is happening here?? Why both == and equals operator returns true? 🙂
both == and equals operator returns true because here only one literal object “Hello” will be created which will be pointed by both the references str and str1 so they are equal in value and references. this would be false if we would have created the objects using new keyword.
You are right. 🙂
equals and == do the same thing