Pitfall using to compare strings

suggest change

A common mistake for Java beginners is to use the == operator to test if two strings are equal. For example:

public class Hello {
    public static void main(String[] args) {
        if (args.length > 0) {
            if (args[0] == "hello") {
                System.out.println("Hello back to you");
            } else {
                System.out.println("Are you feeling grumpy today?");
            }
        }
    }
}

The above program is supposed to test the first command line argument and print different messages when it and isn’t the word “hello”. But the problem is that it won’t work. That program will output “Are you feeling grumpy today?” no matter what the first command line argument is.

In this particular case the String “hello” is put in the string pool while the String args[0] resides on the heap. This means there are two objects representing the same literal, each with its reference. Since == tests for references, not actual equality, the comparison will yield a false most of the times. This doesn’t mean that it will always do so.

When you use == to test strings, what you are actually testing is if two String objects are the same Java object. Unfortunately, that is not what string equality means in Java. In fact, the correct way to test strings is to use the equals(Object) method. For a pair of strings, we usually want to test if they consist of the same characters in the same order.

public class Hello2 {
    public static void main(String[] args) {
        if (args.length > 0) {
            if (args[0].equals("hello")) {
                System.out.println("Hello back to you");
            } else {
                System.out.println("Are you feeling grumpy today?");
            }
        }
    }
}

But it actually gets worse. The problem is that == will give the expected answer in some circumstances. For example

public class Test1 {
    public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        if (s1 == s2) {
            System.out.println("same");
        } else {
            System.out.println("different");
        }
    }
}

Interestingly, this will print “same”, even though we are testing the strings the wrong way. Why is that? Because the Java Language Specification (Section 3.10.5: String Literals) stipulates that any two string >>literals<< consisting of the same characters will actually be represented by the same Java object. Hence, the == test will give true for equal literals. (The string literals are “interned” and added to a shared “string pool” when your code is loaded, but that is actually an implementation detail.)

To add to the confusion, the Java Language Specification also stipulates that when you have a compile-time constant expression that concatenates two string literals, that is equivalent to a single literal. Thus:

public class Test1 {
public static void main(String[] args) {
    String s1 = "hello";
    String s2 = "hel" + "lo";
    String s3 = " mum";
    if (s1 == s2) {
        System.out.println("1. same");
    } else {
        System.out.println("1. different");
    }
    if (s1 + s3 == "hello mum") {
        System.out.println("2. same");
    } else {
        System.out.println("2. different");
    }
}
}

This will output “1. same” and “2. different”. In the first case, the \+ expression is evaluated at compile time and we compare one String object with itself. In the second case, it is evaluated at run time and we compare two different String objects

In summary, using == to test strings in Java is almost always incorrect, but it is not guaranteed to give the wrong answer.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents