File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer

 
Sulbha Jan
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Sulbha Jan
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!

I wasnt knowing toString() is overridden in StringBuffer.
 
Zak Guesmia
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right about StringBuffer.

StringBuffer.equals() is the same as ==
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the differnce between equals() and == ??

regards
amit
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic