• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Difference of equals method in string and stringbuffer

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a bunch!!! got it now!
:-)
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But why is the StringBuffer not overriding the equals method ? Is there any specific reason ?
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,

 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Yogesh Gnanapraksam
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too feel mutable/immutable is a point to be considered.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic