Java equals Method vs == Operator

Here 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 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. But most of the time people relate this question with String Object only and answer accordingly.

Before you go into details first let me tell where this method comes from. Object class has nine methods.

  1. clone()
  2. wait()
  3. wait(long)
  4. wait(long, int)
  5. notify()
  6. notifyAll()
  7. toString()
  8. equals()
  9. hashcode()

All classes in Java world inherits these methods. As you can see equals() method is also the part of Object class, hence it will be available to all Java class.

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

 

Behavior with respect String class

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 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? 🙂

<< Different ways to create an object in Java Inner Class >>

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.