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?
            Asked
            
        
        
            Active
            
        
            Viewed 1,615 times
        
    7
            
            
         
    
    
        Mark Rotteveel
        
- 100,966
- 191
- 140
- 197
 
    
    
        Albus Dumbledore
        
- 12,368
- 23
- 64
- 105
- 
                    3String.equals() is efficient. The first thing checked is if string1 == string2. (Actually Object.equals() checks this.) – Skip Head Oct 07 '10 at 21:01
- 
                    But of course! How *stupid* of me. It's a very obvious thing to do. And highly efficient one as you say. Never though of it. *Shame on me!* Thanks! – Albus Dumbledore Oct 07 '10 at 21:26
3 Answers
16
            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
        
- 1,421,763
- 867
- 9,128
- 9,194
- 
                    1Absolutely the right answer. Using '==' because equals() is inefficient is a great example of premature optimization. – DJClayworth Oct 07 '10 at 21:30
- 
                    And String.intern() is slow enough to be useless for performance in most cases. – Darron Oct 07 '10 at 21:30
1
            
            
        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
 
    
    
        Anthony
        
- 9,451
- 9
- 45
- 72
- 
                    2The first comparison will *not* be false because they are *not* separate objects. String literals are interned in Java. – sepp2k Oct 07 '10 at 20:53
- 
                    The first comparison should return false in this case- String string1 = new("Bob"); String string2 = new("Bob"); – stratwine Oct 07 '10 at 21:01
- 
                    I'll edit my responce to make it more clearly defined. I thought I might run into this issue with Java. – Anthony Oct 07 '10 at 21:05
- 
                    1@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 Oct 07 '10 at 21:06
- 
                    1The 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 Oct 07 '10 at 21:29
- 
                    @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 Oct 08 '10 at 00:37
- 
                    @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 Oct 08 '10 at 00:50
0
            
            
        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
        
- 9,673
- 6
- 31
- 51