wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Difference of equals method in string and stringbuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference of equals method in string and stringbuffer" Watch "Difference of equals method in string and stringbuffer" New topic
Author

Difference of equals method in string and stringbuffer

Neelima Mohan
Greenhorn

Joined: May 23, 2010
Posts: 27
public class Myclass {

public static void main(String[] args) {

String s1 = new String("Hello");
String s2 = new String("Hello");

StringBuffer sb1 = new StringBuffer("Hello");
StringBuffer sb2 = new StringBuffer("Hello");

System.out.println(sb1 == sb2);
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(sb1.equals(sb2));

}

}

This returns false, false, true, false. Why is sb1.equals(sb2) returning false.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

StringBuffer doesn't override equals(); its equals() method is the one inherited from java.lang.Object, which returns true if and only if "==" would be true. String, on the other hand, does override equals(); its definition returns true of the two objects contain the same sequence of characters.


[Jess in Action][AskingGoodQuestions]
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Hello and welcome to the forum!

It's false because that's how StringBuffer is defined. In fact, per my review of the API, it does not appear that StringBuffer overrides the equals method from Object as String does. If instead you want to compare Strings held by the two StringBuffers, you could do:



Much luck, and again welcome to JavaRanch!
Neelima Mohan
Greenhorn

Joined: May 23, 2010
Posts: 27
Thanks a bunch!!! got it now!
:-)
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
But why is the StringBuffer not overriding the equals method ? Is there any specific reason ?
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Is there any reason why the StingBuffer and StringBuilder classes don't override the equals method, whereas the String class overrides the equals method.

The only difference i could find out is StringBuffer and StringBuilder is mutable whereas String is immutable,

Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
I dont think (IMHO) it has got to do anything with being mutable or immutable. In my opinion, StringBuffer and StringBuilder are temporary buffers to hold a string while it is in its construction phase - not the finished product. You generally do the comparison operations only at the end or beginning of a string construction, not in-between (again, just my opinion). Hence the creators of StringBuffer and StringBuilder classes might have decided against overriding equals() method.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

I think Dawn is right. These classes are in 90+% of the times used as temporary storage, as a way of building a String (hence the name StringBuilder) more efficiently than using String concatenation. The intermediate results are mostly just that - intermediate results, not important. If you still need to check the contents you can use a simple utility method:
Because it uses CharSequence it accepts not just StringBuffer and StringBuilder but others like String as well. You can then call it as


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yogesh Gnanapraksam
Ranch Hand

Joined: Dec 17, 2009
Posts: 133
I too feel mutable/immutable is a point to be considered.

Aftab Ahmad khan
Greenhorn

Joined: May 27, 2010
Posts: 4
Hello here is little bit demonstration about your question hopeful it may helpful for you,
String Buffer's equals method returns true only when a String Buffer
object is compared with itself. It returns false when compared with any
other String Buffer, even if the two contain the same characters. This
is actually quite a sensible behavior. However, the meaning of equals
is unfortunately not defined very well, and it is inconsistently used
across the Java A PI, so this is a bit confusing.

To compare the String objects that are produced by the String Buffer
objects in their current state, use s1.to String().equals(s2.to String())
instead.

Regards
Aftab


Aftab Ahamad
S.E, NEXT TECH SOLUTION USA
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Yogesh Gnanapraksam wrote:I too feel mutable/immutable is a point to be considered.

I disagree. java.util.Date is mutable but it has overridden equals. Likewise for many other classes, especially in the Collections framework. ArrayList is highly mutable, just as mutable as StringBuilder / StringBuffer, yet it has an overridden equals method (actually inherited from AbstractList).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Welcome to the Ranch Aftab Ahmad khan

The explanation which I read somewhere on the Sun site is that StringBuilder is designed to change rapidly and we don't want the intermediate stages to be compared. Exactly what Dawn and Rob said earlier.

I presume beginners realise one can work out whether the equals() method is overridden: if it is, it appears under "methods", but if not it appears under "methods inherited from Object."
Aftab Ahmad khan
Greenhorn

Joined: May 27, 2010
Posts: 4
Thank you Mr Campbell Ritchie....
Yes Dawn and Rob are absolutely right,you know every buddy have there own way how they explain, well i appreciate coderanch members, they are doing awesome job so for you guys...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
You're welcome
 
wood burning stoves
 
subject: Difference of equals method in string and stringbuffer
 
Similar Threads
StringBuffer
StringBuffer Question???????
arguments to method
String
String and StringBuffer