StringBuilder .equals Java

后端 未结 8 1744
谎友^
谎友^ 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 and StringBuffer not override the equals function of Object class.but string override the equals method. the function of Object is this

    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()));
    

提交回复
热议问题