Difference Between == Operator and equals() Method in Java

In Java, we often use == operator and equals() method to perform comparison operation. However, both are not work in the same way. The difference can be noticed when one tries to compare two objects.

It is one of the frequently asked interview questions as it creates confusion when it is asked with String. Because both behave differently in the situation of strings.

== Operator

== operator is a relational operator that is used for checking equality between two operands. In the case of primitive types, If the values of two variables are equal, the condition becomes true, otherwise false.

When it comes to objects, it checks whether two objects are pointing to the same memory space or not.

Whenever an object is declared with the new keyword, a new memory is created for that very object. In such situation, == operator can be used to verify whether memory location of two objects are same or not

equals()

equals() is a method that comes under the Object class. Since object class is root class, therefore, by default, every class have an equal method with default behaviour.

It checks whether two objects are equals or not, including equality checks for content and where they are referencing to the same memory location or not. It is also possible to override equal method in order to implement desirable behaviour.

Difference between == operator and equals()?

The main difference between the == operator and equals() method is, == always checks whether two objects are pointing to the same memory locations or not, on the other hand, equals() compares where values of two objects are similar or not.

Basis"==" Operatorequals()
DefinitionIt is used to compare two operands, if both are equal, the condition becomes true, otherwise falseIt is used to compare two objects, if both are equal ,then it would return true, otherwise false
ComparisionCompares memory location of two objectsCompares content of two objects
Type It is an operatorIt is a method
OverridingNot allowed on OperatorAllowed

Therefore, when we use == operator on an object such as String class and Wrapper classes such as Integer, Boolean, Byte, would always return true as these classes are immutable.

Meaning, if you have a string str1, holding a value “hi” and created another string str2 having the same value, then Java would simply point str2 to the same memory location where “hi” exist, instead of creating a new memory space.

Java program to show the working of equal operator

public class ImmutableExample
{
public static void main(String[] args) {
String str1 = "hi";
String str2 = "hi";
System.out.println(" When both are point to same object " + str1== str2); // would be true since pointing
String str3 = new String("hi");

System.out.println(""+ str3 == str1); // would be false

}
}

In the above code, str1== str2 would be true as both are pointing to a memory location of “hi”. Whereas, in the case of, str3 == str1, both are not referencing two same locations.

Java program to demonstrate the working of equals() Method

Coming to equal operator, Here is the code to look the difference.

public class ImmutableExample
{
public static void main(String[] args) {
String str1 = "hi";
String str2 = "hi";
System.out.println(str1.equals(str2)); // would be true
String str3 = new String("hi");

System.out.println(str3.equals(str1)); // would be true

}
}

In above code, in both comparisons were made on values of any String and wrapper class object,instead, references. Therefore, both would give you true.

Object Comparison of User Defined Classes

In case of user-defined classes, we require to override the default implementation of equals() method to make it work as per our preference. Because, equality() would check for content and memory location equality . If both are equivalent, only then it would return true.

Java program to override equals() method

public class ProgrammingBooks {
private String bookName;
private String language;

public ProgrammingBooks(String bookName, String language) {
this.bookName = bookName;
this.language = language;
}

@Override
public boolean equals(Object obj) {
ProgrammingBooks anotherObj;
if(obj instanceof ProgrammingBooks) {
anotherObj = (ProgrammingBooks) obj;
return this.bookName.equals(anotherObj.bookName) && this.language.equals(anotherObj.language);
}
return false;
}

public static void main(String[] args) {
ProgrammingBooks book1 = new ProgrammingBooks("Head First Java","Java");
ProgrammingBooks book2 = new ProgrammingBooks("Head First Java","Java");
System.out.println(book1.equals(book2));

}

}

In the above code, we overrode the equal method to get rid of its default implementation. Now, as per requirement, we are checking only the content of the object, if they are the same, it would return true, otherwise false.  Prior to overriding, it was returning false as it was also checking for memory location equality.

Leave a Reply