找到你要的答案

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.

For example:

String fooString1 = new String("foo");
String fooString2 = new String("foo");

// Evaluates to false
fooString1 == fooString2;

// Evaluates to true
fooString1.equals(fooString2);

// Evaluates to true, because Java uses the same object
"bar" == "bar";

But beware of nulls!

== handles null strings fine, but calling .equals() from a null string will cause an exception:

String nullString1 = null;
String nullString2 = null;

// Evaluates to true
nullString1 == nullString2;

// Throws an Exception
nullString1.equals(nullString2);

= =测试对象的引用,equals()测试的字符串值。

有时好像= =比较值,因为java做一些幕后的东西以确保相同的同轴串实际上是同一对象。

例如:

String fooString1 = new String("foo");
String fooString2 = new String("foo");

// Evaluates to false
fooString1 == fooString2;

// Evaluates to true
fooString1.equals(fooString2);

// Evaluates to true, because Java uses the same object
"bar" == "bar";

但要注意空值!

= =处理空弦细,但叫。从一个空的字符串equals()将导致异常:

String nullString1 = null;
String nullString2 = null;

// Evaluates to true
nullString1 == nullString2;

// Throws an Exception
nullString1.equals(nullString2);
answer3: 回答3:

== compares Object reference.

.equals() compares String value.

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

if(a==b) ===> false.

                String Pool
     "test" <-------------------- a

                   Heap
     "test" <-------------------- b

While .equals() always compares value of String so it gives true in both cases

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

String a="test";
String b=new String("test");
if(a.equals(b)) ===> true

So using .equals() is always better.

Hope this will help.

=比较对象引用。

equals()比较字符串的值。

有时=给出比较字符串值的错觉,如下列情况:

String a="Test";
String b="Test";
if(= = b)= = = & gt;真实

这是因为当你创建任何字符串,用于在字符串池字面JVM首先搜索,如果找到一个匹配,同参考将给予新的字符串。正因为如此,我们得到

(= = b)= = = & gt;真实

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

然而,=在下列情况下失败

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

如果(a = b)= = = & gt;假。

                String Pool
     "test" <-------------------- a

                   Heap
     "test" <-------------------- b

同时,equals()总是比较值的字符串使真的在这两种情况下

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

String a="test";
String b=new String("test");
if(a.equals(b)) ===> true

所以使用。equals()总是好的。

希望这将有助于。

answer4: 回答4:

The == operator checks to see if the two strings are exactly the same object.

The .equals() method will check if the two strings have the same value.

操作符检查是否两个字符串完全相同的对象。

的。equals()方法将检查如果两个字符串有相同的价值。

java  string  equality