StringBuilder .equals Java

后端 未结 8 1937
谎友^
谎友^ 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:28

    StringBuilder class doesn't have the implementation of equals() method like one in the String class.

    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.

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

    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.

提交回复
热议问题