StringBuilder .equals Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18565701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:04:48  来源:igfitidea点击:

StringBuilder .equals Java

java

提问by UnderDog

class strb
{

    static public void main(String...string)
    {
         StringBuilder s1 = new StringBuilder("Test");
         StringBuilder s2 = new StringBuilder("Test");

         System.out.println(s1);
         System.out.println(s2);
         System.out.println(s1==s2);
         System.out.println(s1.equals(s2)); //Line 1
         System.out.println(s1.toString()==s2.toString()); //Line 2
         if(s1.toString()==s2.toString())System.out.println("True"); //Line 3
    }

}

And the output is

输出是

Test
Test
false
false

Just have a quick question on .equals.

只是有一个关于 .equals 的快速问题。

Regardless of the object content, does .equalsreturn true only if both the object references point to the same object ?

无论对象内容如何,.equals只有当两个对象引用都指向同一个对象时才返回 true 吗?



EDIT: Now I understand the part about the .equalsbut why does Line 2 and Line 3 also not return true?

编辑:现在我明白了部分,.equals但为什么第 2 行和第 3 行也没有返回true

EDIT: I believe ==looks at the reference variable's address and so s1 and s2 cannot be equal.correct me if my assumption is not right

编辑:我相信==查看引用变量的地址,因此 s1 和 s2 不能相等。如果我的假设不正确,请纠正我

采纳答案by hotforfeature

Yes, StringBuilder does not override Object's .equals() function, which means the two object references are not the same and the result is false.

是的,StringBuilder does not override Object's .equals() function,这意味着两个对象引用不相同,结果为假。

For StringBuilder, you could use s1.toString().equals(s2.toString())

对于StringBuilder,您可以使用s1.toString().equals(s2.toString())

For your edit, you're calling the ==operator on two different String objects. The ==operator will return false because the objects are different. To compare Strings, you need to use String.equals()or String.equalsIgnoreCase()

对于您的编辑,您==在两个不同的 String 对象上调用运算符。==由于对象不同,运算符将返回 false。要比较字符串,您需要使用String.equals()String.equalsIgnoreCase()

It's the same problem you were having earlier

这与您之前遇到的问题相同

回答by tschaible

The default implementation of .equalsfor the Objectclass is as you mentioned.

的默认实现.equalsObject类如你所提到的。

Other classes can override this behavior. StringBuilderis not one of them.

其他类可以覆盖此行为。 StringBuilder不是其中之一。

String is one of them, which overrides it to ensure that the String representations of both objects result in the same sequence of characters. String API

String 就是其中之一,它会覆盖它以确保两个对象的 String 表示产生相同的字符序列。 字符串 API

Refer to the documentation for the specific object in question.

请参阅有关特定对象的文档。

回答by pfrank

Agreed with both of the above responses, but I'm worth noting to actually compare contents you can do:

同意上述两个回复,但我值得注意的是实际比较您可以执行的内容:

System.out.println(s1.toString().equals(s2.toString())); //Line 1

回答by Sotirios Delimanolis

The StringBuilderclass does not provide an overriden equals()method. As such, when that method is called on an instance of StringBuilder, the Objectclass implementation of the method is executed, since StringBuilder extends Object.

StringBuilder类不提供被覆盖的equals()方法。因此,当在 的实例上调用该方法时StringBuilder,将Object执行该方法的类实现,因为StringBuilder extends Object

The source code for that is

源代码是

public boolean equals(Object obj) {
    return (this == obj);
}

Which simply compares reference equality.

这只是比较引用相等性。

回答by Tarsem Singh

for your first answer check @abmitchell 's Answer

对于您的第一个答案,请查看 @abmitchell 的答案

And for your Edit:

而对于您的编辑:

In Java, String is an object and we can't compare objects for value equality by using ==

在 Java 中,String 是一个对象,我们不能通过使用来比较对象的值相等性 ==

==is used for comparing primitives values or object references.

==用于比较基元值或对象引用。

To compare Object values we use equals()in Java

比较我们equals()在 Java 中使用的对象值

回答by very9527

StringBuilderand StringBuffernot override the equals function of Objectclass.but string override the equalsmethod. the function of Objectis this

StringBuilder并且StringBuffer不覆盖Objectclass.but 字符串的 equals 函数覆盖该equals方法。的功能Object是这个

public boolean equals(Object obj) {
    return (this == obj);
    }

you could write your code like this.

你可以这样写你的代码。

System.out.println(s1.toString() == s2.toString());
System.out.println(s1.toString().equals(s2.toString()));

回答by user1923551

StringBuilderclass doesn't have the implementation of equals()method like one in the Stringclass.

StringBuilder类没有equals()像类中那样的方法的实现String

So it executes default Object class functionality, which again checks only for address equivalency, which is not same in this case. so it prints false.

所以它执行默认的 Object 类功能,它再次只检查地址等效性,这在这种情况下是不同的。所以它打印错误。

Note 1: You can use ==operator on objects also, but it simply checks if the address of both the objects are same or not.

注意 1:您也可以==在对象上使用运算符,但它只是检查两个对象的地址是否相同。

Note 2: ==operator plays good role in comparing two String objects created in string constant pool, to find out if it is really creating a new object in string constant pool or not.

注2==运算符在比较字符串常量池中创建的两个String对象时起到了很好的作用,以确定是否真的在字符串常量池中创建了一个新对象。

回答by jade

Check the contract of equalsmethod:

检查equals方法的契约:

it must be consistent (if the objects are not modified, then it must keep returning the same value).

它必须是一致的(如果对象没有被修改,那么它必须保持返回相同的值)。

That's why StringBuilder does not override it regardless of its content.

这就是 StringBuilder 无论其内容如何都不会覆盖它的原因。

Let's take example above.

我们以上面的例子为例。

     StringBuilder s1 = new StringBuilder("Test");
     StringBuilder s2 = new StringBuilder("Test");

Maybe, to you it is expected that s1.equals(s2)returns truedue to current run time values.

也许,对您来说,由于当前的运行时值,预计会s1.equals(s2)返回true

But what about if you change add line:

但是,如果您更改添加行呢:

    s1.append("abc");

Then s1 and s2 will have different string contents and s1.equals(s2)is expected to be false. But it is in contradiction with consistency.

那么 s1 和 s2 将具有不同的字符串内容,s1.equals(s2)预计为false. 但它与一致性相矛盾。