In JDK 1.6, can a String operation equal an operation, can it be replaced with ==?

When I study the source code of some open source products, I find code like:

if (a=="cluser") 

a is a string variable. Is it possible to replace the equals string with == ?

+4
source share
5 answers

You should almost never use == and almost always use equals() . It will only work with == if both lines refer to the same object. There, the intern() method on String returns the same reference for the given string value. String literals are implicitly interned. Only if you have a very good reason, you should use == to compare strings, and even then you need to be very careful.

The only good reason is performance, and very rarely does it matter. Optimize only once , you know for sure that you need to do this. Otherwise, as a rule, it is not worth the hassle. If you look at some kind of open source code, they might have a case where the comparison is performed in a very narrow loop or called very often, and optimization can help. Or it was only prematurely optimized and might have seemed safe.

+6
source

You can use == if two lines are interned. String literals are defined as internals, so if I, say, assign the literal "fred" to the String variable and pass it as a parameter to a method in another class where it compares with "fred", the test == will be true.

Thus, the sample code is not defective, perhaps so.

+4
source

only if the lines were interned. Usually not.

+3
source

From the Java Language Specification, Section 3.10.5 String Literals

Each string literal is a reference to an instance of the String class. String objects have a constant value. String literals, or, more generally, strings that are constant expression values, are β€œinterned” to exchange unique instances using the String.intern method.

Thus, a test program consisting of a 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 compilation unit:

 package other; public class Other { static String hello = "Hello"; } 

outputs the result:

 true true true true false true 

This example illustrates six points:

  • Literal strings in the same class in the same package link to the same string object.
  • Literal strings in different classes in one package are references to the same String object.
  • Literal strings in different classes in different packages are also references to the same String object.
  • The lines computed by the expression constant are computed when the time is compiled, and then processed as if they were literals.
  • Lines computed by concatenation at run time are newly created and therefore different.
  • Result of Explicit Internment The calculated string is the same string as any pre-existing literal string with the same contents.
+1
source

The == operator checks if two objects are exactly the same object. He does not compare what is in the lines.

0
source

All Articles