0

How does a StringBuffer handle strings internally? I tried running the below example and got the answer as "String are unequal".

From what I know is that the equals() returns true if both the strings have the same value. So what is happening in this case?

class StringBufferTest {

public static void main(String[] args) {
    String newString = "HelloWorld";
    StringBuffer buffer = new StringBuffer(newString);

    if (buffer.equals(newString)) {
        System.out.println("Strings are equal");
    } else {
        System.out.println("String are unequal");
    }

  }
}
3
  • 2
    You are comparing a StringBuffer instance to a String instance. Commented Jul 9, 2013 at 16:33
  • 5
    Sure, the equals() method returns true if both the strings have the same value -- and the arguments are both Strings. Commented Jul 9, 2013 at 16:33
  • A String can never be equal() to a StringBuffer (or vice versa) since they are different classes. Commented Jul 9, 2013 at 16:33

5 Answers 5

8

You're comparing an instance of StringBuffer with an instance of String, which won't give the desired results for being different types. Note that StringBuffer will use the plain Object#equals since it doesn't override it (noted by StringBuffer JavaDoc against String#equals that indeed overrides it). You must compare the String content of the buffer, not the object reference of your buffer variable:

if (buffer.toString().equals(newString)) {
    //...
}

Also, from Java 5, it would be better using StringBuilder instead of StringBuffer. More info about this: Difference between StringBuilder and StringBuffer

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

Comments

2

equals( returns false when two objects being compared are not the same type. No matter the contained string, StringBuffer is a different type than String and the comparison will never be true.

buffer.toString.equals(newString) should work.

Comments

0

The equals() contract implies that x.equals(y) returns true if and only if x.getClass() == y.getClass(). In other words, x and y must be instances of the same class. In this case, you are trying to compare a StringBuffer to a String. Comparing instances of two different classes will always be false for a well-behaved equals() method.

In order to do a meaningful comparison, you need to get the contents of the StringBuffer:

buffer.toString().equals(newString)

Comments

0

You are comparing:

if (buffer.equals(newString)) 

Which is actually comparing a StringBuffer object type to a String object type. That will obviously return false.

In order to get true returned from equals method you need to compare objects of same types.

As per official doc:

public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:

It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false.

Comments

0

can not equals a String Object with a StringBuffer Object, use:

buffer.toString().equals(newString)

and better use StringBuilder, because StringBuffer not TheredSafe

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.