views:

101

answers:

3

When should one compare Strings as objects and when should one use their equals method? To make sure, I always use equals, but that doesn't seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use?

Thanks!

+12  A: 

You should almost always use equals. You can be certain that string1 == string2 will work if:

  • You've already made sure you've got distinct values in some other way (e.g. you're using string values fetched from a set, but comparing them for some other reason)
  • You know you're dealing with compile-time string constants
  • You've manually interned the strings yourself

It really doesn't happen very often, in my experience.

Jon Skeet
Thank you Jon, you always put things simply and clearly.
Albus Dumbledore
Absolutely the right answer. Using '==' because equals() is inefficient is a great example of premature optimization.
DJClayworth
And String.intern() is slow enough to be useless for performance in most cases.
Darron
+1  A: 

From what I know of Java, string1==string2 will only be true if the references to those objects are the same. Take a look at the following case

String string1 = new String("Bob");
String string2 = new String("Bob");

string1 == string2; // false, they are seperate objects
string1 = string2;  // asigning string1 to string2 object
string1 == string2; // true, they both refer to the same object
Shynthriir
The first comparison will *not* be false because they are *not* separate objects. String literals are interned in Java.
sepp2k
The first comparison should return false in this case-String string1 = new("Bob");String string2 = new("Bob");
stratwine
I'll edit my responce to make it more clearly defined. I thought I might run into this issue with Java.
Shynthriir
@stratwine: No, this is not true. See http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5: Each string literal is a reference (§4.3) to an instance (§4.3.1, §12.5) of class String (§4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (§15.28)-are "interned" so as to share unique instances, using the method String.intern.
Dirk
The answer is correct, and the first comparison is 'false'. "new String(...)" creates (surprise surprise) a new String, which is not the same object as the interned "Bob".
DJClayworth
@Dirk - Strings created at compile time are interned. Not the Strings that are created on the fly. new operator creates the String at runtime and there's no interning involved here.
stratwine
@stratwine: Seems I misread your comment. I thought that you'd tried to "expand" the poster's original formulation of the initial assignments instead of providing an alternative case to elaborate on. Apologies.
Dirk
A: 

You can only use the == for comparison if you are sure the objects are the same.

For example, this could occur if you had a final static String variable. You could be certain that a comparison would be between the same object.

Stick with the equals for string comparison.

Starkey