wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "StringBuffer" Watch "StringBuffer" New topic
Author

StringBuffer

Sulbha Jan
Ranch Hand

Joined: Jan 02, 2005
Posts: 37
Methods like toLowercase and toUpperCase does not work on StringBuffer object.
But if there is a requirement to change the case for StringBuffer then how to achieve the same?

Thanks
Zak Guesmia
Greenhorn

Joined: May 28, 2004
Posts: 21
simply use a String as a placeholder, do your job, and get back to your StringBuffer or StringBuilder if you use 1.5. StringBuilder is more efficient on most implementations than StringBuffer but it is not thread-safe
e.g.
StringBuffer buffer = new StringBuffer("HELLO");
// or StringBuilder buffer = new StringBuilder("HELLO");
String hello = buffer.toString().toLowerCase();
buffer.setLength(0);
buffer.append(hello);


SCJP1.4, SCWCD1.4, SCBCD1.3, SCDJWS, SCEA(part1), MCP, MCAD.NET, SCJP 5.0 (beta)
Sulbha Jan
Ranch Hand

Joined: Jan 02, 2005
Posts: 37
Thank you!

I wasnt knowing toString() is overridden in StringBuffer.
Zak Guesmia
Greenhorn

Joined: May 28, 2004
Posts: 21
You should know the methods that are common to all classes, those that are in the java.lang.Object
clone, equals, finalize, getClass, hashCode, notify and notifyAll, toString, and the 3 wait(...)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You should know the methods that are common to all classes, those that are in the java.lang.Object
clone, equals, finalize, getClass, hashCode, notify and notifyAll, toString, and the 3 wait(...)

What you said is true, but the versions of toString() and equals() inherited from Object are usually not the ones you want.

Object.toString() would not give you the characters in the StringBuffer, but StringBuffer overrides that toString() with a version that does.

On the other hand, Object.equals() is not overridden in StringBuffer, so comparing two StringBuffer objects with equals() tests for the same object, like ==, rather than comparing the characters in the two StringBuffer objects.


Mike Gershman
SCJP 1.4, SCWCD in process
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Greate Mike ...

equals() method is only overridden by String class . Other than that it is same as == operator . Am I right ???
[ January 08, 2005: Message edited by: rathi ji ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You are right about StringBuffer.

StringBuffer.equals() is the same as ==
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
what is the differnce between equals() and == ??

regards
amit


Thanks and Regards, Amit Taneja
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Amit-
The equals() methods is a special method that is implemented in the base of all classes (Object class), this method (if not overridden in a subclass) has the same effect of the == operator which tests whether two references point to the same object.

Some classes, like the String class override this method to achieve the functionality of actually comparing the "value" of String objects instead of just comparing references. However, StringBuffer class doesn't override this method, therefore when you call equals() method on a StringBuffer class you are actually calling the equals() method in the Object class which compares references.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer