2

how to compare sb and s

StringBuffer sb= new StringBuffer("Hello");
String s= "Hello";

s.equals(sb.toString()) is giving result as false.

3
  • it prints true for me when I run your code. Are you sure what you think is going on is actually going on? Commented Dec 13, 2011 at 18:07
  • Please post the actual code you're using by cutting and pasting. Don't retype your code here. Commented Dec 13, 2011 at 18:09
  • Try by using equalsIgnoreCase(), if it goes true then there is something about the casing of one of the values. Commented Dec 13, 2011 at 18:12

5 Answers 5

12

Unable to reproduce:

public class Test {
    public static void main(String[] args) {
        StringBuffer sb= new StringBuffer("Hello");
        String s= "Hello";
        System.out.println(s.equals(sb.toString())); // Prints true
    }
}

If you think the two values are really the same, please post a similar short but complete program demonstrating the problem. I suspect you'll find the problem is elsewhere - such as some invisible characters in the string or StringBuffer.

Sign up to request clarification or add additional context in comments.

Comments

1

It is definitely returning true for me. As the content of both strings (the String object you created and the String object returned by the StringBuffer toString()) are the same, the equals() method shouldn't return false.

Comments

0

It will give false, when you will able to compare without using toString() on StringBuffer Object.

Like:

     StringBuffer s=new StringBuffer("abc");
     String s1="abc";
     System.out.println("   "+s1.equals(s));

Comments

0

use s.contentEquals(sb); This will work .

4 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post. - From Review
@mjp66 This does not provide an answer ... because? Since you seem to be new to this review queue please read this: meta.stackexchange.com/questions/185073/…
@Tom You're right, I flubbed here. I often post a quick one-line answer as a comment. I flagged it as NaA as it doesn't elaborate (my mistake, I thought these types of answers were frowned upon). Thanks!
@mjp66 You have to differ between "not an answer" (something like a new question, spam or "thanks") and bad answers (incomplete or broken code snippets; wouldn't solve the problem). The first one needs flags and the second one needs comments and/or downvotes. So in this case: a comment about the missing explanation (and maybe a downvote) would be correct to "handle" this. Btw: a lot of people handle answers incorrectly, so no worries, that happens. And this is also nice to read: meta.stackexchange.com/questions/225370/….
0

Equals and == will not work for comparision of StringBuffer and String.

== will work only for comparisions among the same non-collections [ integers, characters etc.]

equals method will work only for comparisons among the same collections [ array, string, objects etc.]

Note : Comparisions between different collections or non-collection leads to error.

class ClassName{

    public static void main(String[] args) {

        // STRING VS STRING BUFFER

        String strg = "Hello";
        StringBuffer strgBuf = new StringBuffer(strg);

        //if(strg == strgBuf ) //=> error: incomparable types: String and StringBuffer
        
        if(strg.equals(strgBuf)) 
            System.out.println("strg.equals(strgBuf) is true");
        else 
            System.out.println("strg.equals(strgBuf) is false") ;
        //=> strg.equals(strgBuf) is false

        // 2 STRINGS COMPARED
        if(strg.equals(strgBuf.toString())) 
            System.out.println("strg.equals(strgBuf.toString()) is true"); 
        else 
            System.out.println("strg.equals(strgBuf.toString()) is false");
        //=> strg.equals(strgBuf.toString()) is true


        // STRING BUFFER VS STRING BUFFER

        StringBuffer strgBuf2 = new StringBuffer(strg);

        if(strgBuf2 == strgBuf) 
            System.out.println("strgBuf2 == strgBuf is true");
        else 
            System.out.println("strgBuf2 == strgBuf is false") ;
        //=> strgBuf2 == strgBuf is false

        if(strgBuf2.equals(strgBuf)) 
            System.out.println("strgBuf2.equals(strgBuf) is true");
        else 
            System.out.println("strgBuf2.equals(strgBuf) is false") ;
        //=> strgBuf2.equals(strgBuf) is false
        
        // 2 STRINGS COMPARED
        if(strgBuf2.toString().equals(strgBuf.toString())) 
            System.out.println("strgBuf2.toString().equals(strgBuf.toString()) is true");
        else 
            System.out.println("strgBuf2.toString().equals(strgBuf.toString()) is false") ;
        //=> strgBuf2.toString().equals(strgBuf.toString()) is true
        

        // STRING VS STRING

        String strg2 = new String(strg);

        
        if(strg == strg2) 
            System.out.println("strg == strg2 is true");
        else 
            System.out.println("strg == strg2 is false") ;
        //=> strg == strg2 is false

        if(strg.equals(strg2)) 
            System.out.println("strg.equals(strg2) is true");
        else 
            System.out.println("strg.equals(strg2) is false") ;
        //=> strg.equals(strg2) is true
        

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.