Thursday, August 19, 2010

String comparison in Java

String class is an important API in Java but this is the most confusing also when asked in Interviews
regarding its comparison with another String. Here I am discussing the same.

The difference between == and equals(): Both operators can be used in Java, but the second one is a method.

"==" This is called the "Equal to" operator and is used to compare "primitive" types or check if 
you have equal object references(or do they refer to the same object in the heap). There are 8 primitive 
types in Java and you can usually identify them: 
byte, short, int, long, float, double, boolean, char

When used with Strings, one might assume that you are making a comparison with the String values, but no. 
We mentioned above that "Equal to" operator checks for object references when used with objects.

So let's assume the following code:
String s1 = "STRING ME";
String s2 = "STRING ME";
System.out.println("s1 == s2 is " + (s1 == s2));

This would output:
s1 == s2 is true

Why? The JVM does some optimization step with Strings(i.e the Strings get "pooled"). Basically, the JVM 
makes you point to the same Object reference that is pooled in the heap.

Let's make some modification with the Strings
s1 += "2";
s2 += "2";
System.out.println("s1: " + s1);
System.out.println("s2: " + s2);
System.out.println("s1 == s2 is " + (s1 == s2));

The output is now:
s1: STRING ME2
s2: STRING ME2
s1 == s2 is false

Why? Because Strings are "immutable", new Object references are actually created. The String was not actually 
modified, it is a new String object. They are now different object references.

Now, what most of us usually want is "OBJECT EQUALITY", and that's what the next thing is for "equals()"
All objects can use and override the equals() method. Any instance of a class you use or created automatically 
inherits this method. Without overriding, you use the default implementation of the Object class which is not 
very helpful. This works differently when used with the java.lang.String class as it does some character 
comparisons. You cannot override this method in String because String is a final class and cannot be extended.

Using a similar set of code, let's try to make modifications with the Equal To operator, for the String references 
to use the equals() method.

String s1 = "STRING ME";
String s2 = "STRING ME";
System.out.println("s1 == s2 is " + (s1.equals(s2) ) ); //true

s1 += "2";
s2 += "2";
System.out.println("s1 == s2 is " + (s1.equals(s2) ) ); //true
//Both will display "true".

So a final complete example with some added test cases:
----------------------------
public class StringAnother {
    public static void main(String[] args) {
        String s1="Hello";
        String s2=new String("Hello");
        String s3="Hello";
        String s4=new StringBuffer("Hello").toString();
        String s5="Hel"+"lo";
        
        System.out.println("s1==s2: "+(s1==s2));
        System.out.println("s1==s3: "+(s1==s3));
        System.out.println("s1==s4: "+(s1==s4));
        System.out.println("s1==s5: "+(s1==s5));
                
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s4));
        System.out.println(s1.equals(s5));
    }
}
/////Output:
s1==s2: false
s1==s3: true
s1==s4: false
s1==s5: true
true
true
true
true
----------------------------END--------------------------

No comments:

Post a Comment