In JDK 1.6, can String equals operation can be replaced with ==?

前端 未结 5 981
谎友^
谎友^ 2020-12-21 04:10

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

if (a==\"cluser\")

a is a String variable. Can a St

5条回答
  •  春和景丽
    2020-12-21 05:14

    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.

提交回复
热议问题