StringBuilder .equals Java

后端 未结 8 1746
谎友^
谎友^ 2020-12-08 20:09
class strb
{

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


        
相关标签:
8条回答
  • 2020-12-08 20:49

    The default implementation of .equals for the Object class is as you mentioned.

    Other classes can override this behavior. StringBuilder is not one of them.

    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

    Refer to the documentation for the specific object in question.

    0 讨论(0)
  • 2020-12-08 20:50

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

    For StringBuilder, you could use 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()

    It's the same problem you were having earlier

    0 讨论(0)
提交回复
热议问题