tags:

views:

211

answers:

6

Whenever I use equals() method with two different StringBuffer objects, I get the result as false, but when I use equals() method with two different String objects, I get the result as true. Why?

    String s=new String("434");
    String s1=new String("434");

    System.out.println(s.equals(s1));//true

   StringBuffer s=new StringBuffer("434");
   StringBuffer s1=new StringBuffer("434");

   System.out.println(s.equals(s1));//false
+14  A: 

StringBuffer does not override equals(). As such, Object.equals() is called, which compare the object identity (the memory address). String does override equals and compare the content.

Thierry-Dimitri Roy
OOC, how *DO* you get to use the default implementation of equals for string where the actual objects are compared?
Shoko
Use the `==` operator to compare the object references for equality.
seh
Are there any specific reason StringBuffer.equals() method is not implemented?
Venkat
probably because the implementation of StringBuffer might make equals comparison non-trivial without calling toString() and as such its better to make the cost of calling toString() explicit by forcing a client to call it to perform equality checks.
Jherico
+1  A: 

String s.equals will use the string table to compare the actual strings where as the StringBuffer sb.equals will just use the default implementation of the equals method and just compare the object pointers.

JERiv
+2  A: 

At least in my version of the JDK (Sun 1.6), StringBuffer does not implement an equals() method. This means it inherits Object's equals() method, which is the same as ==

If you really want to test two StringBuffers for equality, you could call x.toString().equals(y.toString())

Steven Schlansker
+3  A: 

StringBuffer does not override Object#equals(), so you're experiencing reference identity-based checks rather than value-based checks. As these StringBuilder instances are distinct, each with different memory locations, the base Object#equals() implementation will always return false.

Here's the definition as of Java 6:

public boolean equals(Object obj) {
  return (this == obj);
}

See the problem?

seh
In this whole conversation... i could understand only your answer.
Hari
+2  A: 

If what you are looking to do is compare the String representations of the two StringBuffer objects, then what you want to do is:

StringBuffer sb1 = new StringBuffer("434");
StringBuffer sb2 = new StringBuffer("434");
if (sb1.toString().equals(sb2.toString())) {
  // whatever you want to do if they're equal
} else {
  // whatever you want to do if they're not
}

Otherwise, you're comparing for equality of the two StringBuffer objects, not their contents -- in other words, executing Object#equals(), not (the non-existent) StringBuffer#equals().

delfuego
Beware that each of these calls to `StringBuffer#toString()` creates a fresh copy of the underlying character array. If you're going to need access to either of the buffers as `String`s later, it's good form to save the references from this first call to `toString()` and reuse them.
seh
+2  A: 

equals only returns true on StringBuffer objects when the two objects are the same. To compare StringBuffers the way you want, use this:

System.out.println(s.toString().equals(s1.toString());
Chris Williams