Sunday, November 23, 2014

String comparison in Java. == vs equals method.

Just not to make everything Object oriented, primitives has been retained in Java. To resolve performance issues and to keep it as simple as it is, primitives has been introduced in most using OOP language till date. Lets just not discuss all the primitive concepts and sticking to the heading of this articles lets discuss about our favourite and peculiar Object, STRING.

We all know that String is very special in Java. Designers of Java took special care on Sting while creating the most used Java class till date. If you write the literal ""  in your code somewhere, you just created a String in simplest way. You can create a String object in both Object and primitive style
 String primitiveStyle= "";  
 String objStyle= new String("String in objective style");
Before creating String's you must know that String is immutable. Designers made String immutable in Java for several reasons like Security, Thread safety and for String Pool purposes.

We gotta freedom of Creating strings in primitive style (""), this leads to comparison problems. Though we are using/declaring in String's in primitive style, still they are Objects. You'll get your desired results then and then you are aware of how == works on Objects.

If you are sure that both the string's belongs to same reference use double equals operator. If you are not sure of the references and want to compare them, use
 yourstring.equals(otherString)  
The method to compare strings is equals(), not the == operator. The reason is that == just compares object references. Where as  .equals() of String class checks actual content equality. equlas() method works, because String class's  equals() method over-ridden in such a way that the it checks the passed String objects value is same as current Object and gives desired result.

Look at the source code of equals() method from Open JDK 1.7 :
public boolean equals(Object anObject) {  
     if (this == anObject) {  
       return true;  
     }  
     if (anObject instanceof String) {  
       String anotherString = (String) anObject;  
       int n = value.length;  
       if (n == anotherString.value.length) {  
         char v1[] = value;  
         char v2[] = anotherString.value;  
         int i = 0;  
         while (n-- != 0) {  
           if (v1[i] != v2[i])  
               return false;  
           i++;  
         }  
         return true;  
       }  
     }  
     return false;  
   }  
If you look carefully, the equals() method first checks the references are not, if not then proceeds with equality check of inside content(keep in mind that String in Java backed by char array). From that one can simply state that always use equals() method that check for both == and equality. Look at the below examples
   String s1 = "Hello";       // String literal  
   String s2 = "Hello";       // String literal  
   String s3 = s1;          // same reference  
   String s4 = new String("Hello"); // String object  
   String s5 = new String("Hello"); // String object  
If you compare above String's you'll realize that
  s1 == s2 == s3 but s4 != s5    
  Where as    
  anyOfAbove.equals(anyOtherOfAbove); //true
and if you want to compare regardless of case
yourString.equalsIgnoreCase("otherCasedifferentString"); 

Post a Comment