Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

StringBuffer equals method

Thread view: 
priyom - 28 Nov 2006 06:52 GMT
Hi,

I found the following behaviour in StringBuffer and StringBuilder
equals method.

StringBuffer sb1 = new StringBuffer("ABC");
StringBuffer sb2 = new StringBuffer("ABC");//It can be any string same
as above
System.out.println(sb1.equals(sb2)); //this prints false!

Could someone please explain this behaviour.

Thanks in advance,
Priyom
Andrew Thompson - 28 Nov 2006 07:18 GMT
<sscce>
class TestEquals {
  public static void main(String[] args) {
     StringBuffer sb1 = new StringBuffer("ABC");
     //It can be any string same as above
     StringBuffer sb2 = new StringBuffer("ABC");

     // compare the StringBuffer's for equality
     //this prints false
     System.out.println(sb1.equals(sb2));
     // compare the StringBuffer's *contents*
     // for equality   ;-)
      //this prints true
     System.out.println(sb1.toString().
        equals(sb2.toString()));
  }
}
</sscce>

> Could someone please explain this behaviour.

What is your explanation, based on the above code?

Andrew T.
redbox - 28 Nov 2006 07:47 GMT
>StringBuffer 's equals method inherited from class java.lang.Object
only if sb1 and sb2 refer to the same object return true; the same to
operator "==";
class String equals methode is overridded ;
Andrew Thompson - 28 Nov 2006 08:34 GMT
> >StringBuffer 's equals method inherited from class java.lang.Object
> only if sb1 and sb2 refer to the same object return true; the same to
> operator "==";
>  class String equals methode is overridded ;

Note that the addition of a '>' character to the beginning of
your first statement makes it appear (at a glance) that it
was made by someone else, someone that you are quoting.

Andrew T.
priyom - 29 Nov 2006 11:14 GMT
> <sscce>
> class TestEquals {
[quoted text clipped - 20 lines]
>
> Andrew T.

Well my question was actually regarding the implementation of "equals"
method of StringBuffer and not String. I was under the impression that
in equals implementation, the contents of the StringBuffer i.e. the
String value inside the StringBuffer determines the equality (but
apparently it is not so).

Regarding the above example, two Strings check their contents if they
are meaningfully equivalent and if so return true, as above.
That is the reason I was confused regarding the StringBuffer behaviour
which seemed to be different from that of String or some other standard
implemntations like Integer, Float, etc.

I just checked and found that StringBuffer class in fact does not
override Object.equals. Hence two StringBuffer objects would be equal
only if they refer to the same object.

Thanks for your help.
Lew - 01 Dec 2006 23:19 GMT
> I just checked and found that StringBuffer class in fact does not
> override Object.equals. Hence two StringBuffer objects would be equal
> only if they refer to the same object.

Which is exactly what the other respondents told you.

- Lew


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.