wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes StringBuffer & StringBuilder and equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringBuffer & StringBuilder and equals()" Watch "StringBuffer & StringBuilder and equals()" New topic
Author

StringBuffer & StringBuilder and equals()

Phillip Wells
Greenhorn

Joined: Mar 25, 2010
Posts: 15
Hi all,
I hope you are well.
Am I correct in thinking that the StringBuffer and StringBuilder classes DO NOT override the Object equals() method? So their versions of equals() uses "=="? Is this a valid work around?
I am away from my normal PC at present and do not have a JDK installed on this one or I'd check it out myself.

stringbuffer1 = "hi"';
stringbuffer2 = "hi";
System.out.println((stringbuffer1.toString()).equals(stringbuffer2.toString())); // will it print true?

Is it the same situation for StringBuilder?


SCJP 6.0
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Phillip Wells wrote:Am I correct in thinking that the StringBuffer and StringBuilder classes DO NOT override the Object equals() method?

If you look at the API documentation for those classes you'll see that they both don't override equals(), so they inherit the default equals() from Object, which compares objects using ==.

If you want to check whether two StringBuffers or StringBuilders have the same content, then your workaround (calling toString() on them) is valid.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Phillip Wells
Greenhorn

Joined: Mar 25, 2010
Posts: 15
Excellent, thanks very much Jesper.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

i understand everything what there in tutorial,,,,,,,,,,,,,,,,

but i am not able to point out the difference in between the two............
stringBuffer and StringBuilder


SCJP6.0,My blog Ranchers from Delhi
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385
Methods in StringBuilder are not thread safe.


SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
Phillip Wells
Greenhorn

Joined: Mar 25, 2010
Posts: 15
Hi Shanky,
The difference is that StringBuffer's important methods are synchronized, StringBuilder's are not. Lol "snap" Siva
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
So be sure to use StringBuilder unless you are working with shared data.
In that case, use StringBuffer.

Jim ... ...


BEE MBA PMP SCJP-6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

You may now be wondering why we have two classes that do almost the same (StringBuffer and StringBuilder):

StringBuilder is a new class, added in Java 5. It (more or less) replaces StringBuffer, and you should always prefer StringBuilder above StringBuffer - StringBuffer should be regarded as a legacy class.

The difference is, as already said, that most methods in StringBuffer are synchronized, while they are not in StringBuilder. The engineers at Sun realized that for most applications, synchronization is not necessary, but it does add some runtime overhead.

This is similar to Vector and ArrayList, or Hashtable and HashMap.
Jim Hoglund wrote:So be sure to use StringBuilder unless you are working with shared data.
In that case, use StringBuffer.

To make this more precise: If you use it from multiple threads at the same time (which you almost never do), use StringBuffer. In all other cases (almost always), use StringBuilder.
 
 
subject: StringBuffer & StringBuilder and equals()