Saturday, 21 December 2013

Equals method in Java

Equals method is defined in OBJECT class and overridden in STRING class.
The equals method is declared in Object class and hence is inherited by all classes in Java. The purpose of equals method is to provide logical equality. The default implementation of equals method is:
1.public boolean equals(Object obj) {
2.return (this == obj);
3.}
Thus the default behavior of equals method is same as == operator. 

equals method in String class checks if the two strings have same characters or not. But this check is performed intelligently. The algorithm for checking the Strings for equality involves:

a) If the String objects are equals as per == operator, true is returned.
b) If the == returns false then each character of both the strings are compared and if a difference if found, immediately false is returned.
c) If after comparing all characters of both the strings, no difference is found in characters and length of strings, true is returned.

Please note that the equals method in String class in Java is case-sensitive. The complete code for equals method is reproduced here for reference:

01.public boolean equals(Object anObject) {
02.if (this == anObject) {
03.return true;
04.}
05.if (anObject instanceof String) {
06.String anotherString = (String)anObject;
07.int n = count;
08.if (n == anotherString.count) {
09.char v1[] = value;
10.char v2[] = anotherString.value;
11.int i = offset;
12.int j = anotherString.offset;
13.while (n-- != 0) {
14.if (v1[i++] != v2[j++])
15.return false;
16.}
17.return true;
18.}
19.}
20.return false;
21.}

The ‘==’ operator is expected to check the actual object instances are same or not.
For example, lets say, you have two String objects and they are being pointed by two different reference variables s1 and s2.
1s1 = new String("abc");
2s2 = new String("abc");
Now, if you use the “equals()” method to check for their equivalence as
1if(s1.equals(s2))
2     System.out.println("s1.equals(s2) is TRUE");
3else
4     System.out.println("s1.equals(s2) is FALSE");
You will get the output as TRUE as the ‘equals()’ method check for the content equality.
Lets check the ‘==’ operator..
1if(s1==s2)
2     System.out.printlln("s1==s2 is TRUE");
3   else
4     System.out.println("s1==s2 is FALSE");
Now you will get the FALSE as output because both s1 and s2 are pointing to two different objects even though both of them share the same string content. It is because of ‘new String()’ everytime a new object is created.
Try running the program without ‘new String’ and just with
1String s1 = "abc";
2String s2 = "abc";
You will get TRUE for both the tests. 
By definition, the objects are all created on the heap. When you create an object, say,
1Object ob1 = new SomeObject();
2Object ob2 = new SomeObject();
We have 2 objects with exactly the same contents, lets assume. We also have 2 references, lets say ob1 is at address 0×1234 and ob2 is at address 0×2345. Though the contents of the objects are the same, the references differ.
Using == compares the references. Though the objects, ob1 and ob2 are same internally, they differ on using this operation as we comare references. ob1 at address 0×1234 is compared with ob2 at address 0×2345. Hence, this comparison would fail.
object.equals() on the other hand compares the values. Hence, the comparison between ob1 and ob2 would pass. Note that the equals method should be explicitly overridden for this comparison to succeed.
The following two tabs change content below.
    Reference:
http://java.dzone.com/articles/how-string-equals-method-works
http://www.javabeat.net/what-is-difference-between-equals-and/