Sunday, April 22, 2007

People still don't get String comparison

How many times we have seen String comparison done using == or != operators?! Yet, people still get it wrong.

I have seen some blog posts recently where the authors had a good intention to show a case where hard-coding values went wrong. Unfortunately, they fell into the common mistake of comparison of String references.

So, if you really need to compare two String using == or != make sure you call String.intern() method before making comparison. Otherwise, always prefer String.equals(String) for String comparison.

8 comments:

Anonymous said...

IF you compare a string with == to a "static final String" var it's better than .equals because all "static final String" are intern. Also don't use intern for every string comparison because of the pooling.

Anonymous said...

Of course this is why CheckStyle, PMD or FindBugs are important tools.

Unknown said...

Very true!

I use FindBugs on a daily basis, occasionally PMD. I can tell you that these little helpers helped me a great deal to understand Java better. These tools also helped to eliminate bunch of nasty bugs in our projects that were creeping in the code otherwise undetected.

I believe that these tools hand in hand with some good code coverage tools can help us (developers) produce code with a high level of quality.

Anonymous said...

Man, both posts you think contain mistakes (incorrect string comparison) in fact are absolutely valid. It was not Java code, it is just some kind of pseudo-language.

Unknown said...

Maybe there must be a shift between = and equals.a new JSR!
change == semantic to equals
change equals to sameas

If just from this point for view, java creators are stupid. because all persons and all people need more force and thought to choicing and typing, And THIS IS BIG RESOURCE WASTING.

just soso.

maybe my a language should present now.

Anonymous said...

It's an easy mistake to make, but its about the same as using == to compare two "char *" in C/C++.

Beginners will always make beginner mistakes. That is why they are beginner mistakes :-).

A much more common problem is incorrect synchronization in multi-threaded code.

Anonymous said...

When many people have a problem with a programming language, it's not the people, it's the programming language. Why make java string handling so difficult and non-intuitive? To appease a purist somewhere? Make the programmer's job easier, not harder.

Christian Poecher said...

@Daniel: It's (usually) not ok to compare Strings with == even if it is static final.

Check that code here:

public class StringTest {
static final String S1 = "S1";
String s2 = new String(S1);

public void go() {
System.out.println(s2.equals(S1));
System.out.println(s2 == S1);
}

public static void main(String[] args) {
new StringTest().go();
}
}

Output is true, false.

new String(String s) creates a new Object and therefore has a different reference (at least with Sun VMs).


Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.