Friday, July 29, 2016

When To Use “==”, equals () and hashCode () On Strings

“==” operator compares the two objects on their physical address. That means if two references are pointing to same object in the memory, then comparing those two references using “==” operator will return true. For example, if s1 and s2 are two references pointing to same object in the memory, and then invoking s1 == s2 will return true. This type of comparison is called “Shallow Comparison”.

equals() method, if not overridden, will perform same comparison as “==” operator does i.e. comparing the objects on their physical address. So, it is always recommended that you should override equals() method in your class so that it provides field by field comparison of two objects. This type of comparison is called “Deep Comparison”.

In java.lang.String class, equals() method is overridden to provide the comparison of two string objects based on their contents. That means, any two string objects having same content will be equal according to equals() method. For example, if s1 and s2 are two string objects having the same content, then invoking s1.equals(s2) will return true.

hashCode() method returns hash code value of an object in the Integer form. It is recommended that whenever you override equals() method, you should also override hashCode() method so that two equal objects according to equals() method must return same hash code values. This is the general contract between equals() and hashCode() methods that must be maintained all the time.

In java.lang.String class, hashCode() method is also overridden so that two equal string objects according to equals() method will return same hash code values. That means, if s1 and s2 are two equal string objects according to equals() method, then invoking s1.hashCode() == s2.hashCode() will return true.

Let’s apply these three methods on string objects and try to analyze their output.

Define two string objects like below,
String s1 = "JAVA";
String s2 = "JAVA";

Now apply above methods on these two objects.
s1 == s2 —> will return true as both are pointing to same object in the constant pool.
s1.equals(s2) —> will also return true as both are referring to same object.
s1.hashCode() == s2.hashCode() —> It also returns true.
This type of comparison is straight forward. There is no speculation about this comparison. 

Let’s define the string objects like below,
String s1 = new String("JAVA");
String s2 = new String("JAVA");

s1 == s2 —> will return false because s1 and s2 are referring to two different objects in the memory.
s1.equals(s2) —> will return true as both the objects have same content.
s1.hashCode() == s2.hashCode() —> It will also return true because two equals string objects according to equals() method will have same hash code values.

Comparing the string objects defined like below will also give same result as the above.
String s1 = "JAVA";
String s2 = new String("JAVA");

s1 == s2 —> will return false because s1 and s2 are referring to two different objects in the memory.
s1.equals(s2) —> will return true as both the objects have same content.
s1.hashCode() == s2.hashCode() —> It will also return true.

Conclusion:

When you want to check the equality of two string objects on their physical existence in the memory, then use “==” operator. If you want to check the equality of two string objects depending upon their contents, then use equals() method..

No comments:

Post a Comment