In JDK 1.6, can String equals operation can be replaced with ==?
As I study the source code of some open s开发者_C百科ource products, I find code like:
if (a=="cluser")
a
is a String variable. Can a String equals
operation be replaced with ==
?
You should almost never use ==
and almost always use equals()
. It will only work with ==
if both strings reference the same object. There's an intern()
method on String
to return the same reference for a given string value. String literals are implicitly interned. Only if you have a very good reason should you use ==
for string comparison, and even then you need to be very careful.
The only good reason is performance, and very rarely will it matter. Only optimize once you know for sure that you need to do so. It's otherwise normally not worth the hassle. If you're looking at some open source code, they might have a case whereby the comparison is in a very tight loop or called very frequently and the optimization can help. Or it was just prematurely optimized and maybe seemed safe.
You can use == if the two strings are interned. String literals are defined to be interned, so if I, say, assign the literal "fred" to a String variable and pass it as a parameter to a method in another class where it's compared to "fred", the == test will be true.
So the specimen code is not certainly defective, just probably so.
only if the strings have been 'interned'. Normally no.
From the Java Language Specification, section 3.10.5 String Literals
Each string literal is a reference to an instance of class String. String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions are "interned" so as to share unique instances, using the method String.intern.
Thus, the test program consisting of the compilation unit:
package testPackage;
class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((other.Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}
class Other { static String hello = "Hello"; }
and the compilation unit:
package other;
public class Other { static String hello = "Hello"; }
produces the output:
true true true true false true
This example illustrates six points:
- Literal strings within the same class in the same package represent references to the same String object.
- Literal strings within different classes in the same package represent references to the same String object.
- Literal strings within different classes in different packages likewise represent references to the same String object.
- Strings computed by constant expressions are computed at compile time and then treated as if they were literals.
- Strings computed by concatenation at run time are newly created and therefore distinct.
- The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.
The == operator checks to see if two objects are exactly the same object. It doesn't compare what's in the strings.
精彩评论