Q：How do I compare strings in Java?

Q：我如何比较字符串在java？

I've been using the == operator in my program to compare all my strings so far.
However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.

Is == bad? When should it and should it not be used? What's the difference?

I've been using the == operator in my program to compare all my strings so far.
However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.

是坏的吗？什么时候该用，不该用？有差别吗？

answer1：

回答1：

== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they are logically "equal").

Objects.equals() checks for nulls before calling .equals() so you don't have to (available as of JDK7, also available in Guava).

Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.equals().

// These two have the same value
new String("test").equals("test") // --> true
// ... but they are not the same object
new String("test") == "test" // --> false
// ... neither are these
new String("test") == new String("test") // --> false
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false

You almost always want to useObjects.equals(). In the rare situation where you know you're dealing with interned strings, you can use ==.

=引用相等的测试（它们是否是同一对象）。

。equals()值相等性测试（无论是逻辑上的“平等”）。

对象。equals()检查零点之前调用。equals()所以你不用（可作为JDK7，也可在番石榴）。

因此，如果你想测试是否两个字符串有相同的值，你可能会想使用的对象。equals()。

// These two have the same value
new String("test").equals("test") // --> true
// ... but they are not the same object
new String("test") == "test" // --> false
// ... neither are these
new String("test") == new String("test") // --> false
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false

你总是想useobjects。equals()。在你知道你处理拘留字符串的罕见的情况，你可以用= =。

answer2：

回答2：

== tests object references, .equals() tests the string values.

Sometimes it looks as if == compares values, because Java does some behind-the-scenes stuff to make sure identical in-line strings are actually the same object.

Sometimes == gives illusions of comparing String values, as in following cases:

String a="Test";
String b="Test";
if(a==b) ===> true

This is a because when you create any String literal, the JVM first searches for that literal in String pool, and if it finds a match, that same reference will be given to the new String. because of this, we get

(a==b) ===> true

String Pool
b -----------------> "test" <-----------------a

However, == fails in following case

String a="test";
String b=new String("test");
if (a==b) ===> false

in this case for new String("test") the statement new String will be created in heap that reference will be given to b, so b will be given reference in heap not in String Pool.
Now a is pointing to String in String pool while b is pointing to String in heap, because of that we are getting

in this case for new String("test") the statement new String will be created in heap that reference will be given to b, so b will be given reference in heap not in String Pool.
Now a is pointing to String in String pool while b is pointing to String in heap, because of that we are getting