Using a StringBuffer is more efficient than using a String because a
StringBuffer can be appended to.  If a String is used, all of the contents
in the String must be copied each time it is created.  With a StringBuffer,
you re-use the same object as you append to it.  When using a String, you
are creating (and destroying) String objects each time you do an append.
With a StringBuffer, you can also easily do a string insertion.

-Richard

-
-----Original Message-----
From: B R Nair [mailto:[EMAIL PROTECTED]]
Sent: Sunday, July 30, 2000 8:29 PM
To: [EMAIL PROTECTED]
Subject: Re: Proprtionate String Padding


Thanks a lot Santosh, Naresh & Richard for your invaluble input.

"Strings are constant; their values cannot be changed after they are
created. String buffers support mutable strings." &  "A string buffer is
like a String, but can be modified."

Whenever I read the above Java lanuage documentation, I am a lot more
confused.
If a string is constant, I should not be able to change its value after it's
created. But I can change the value of the string at my will (My experiments
show it *). Then, why we should we use StringBuffer instead of string when
both give the same result?


Thanks and regards
BRN.

*
public class Strpad
{
public static void main(String[] Arg)
{
  String s = "String";
  String a = s;
  String b = "Buffer";
  String m = "mutable";
  String p = "padding -> ";
  String c = s+ " " + p;
  for (int i = 1;i<11;i++)
   c = c + i;
  System.out.println(c);
  System.out.println();
  StringBuffer sb = new StringBuffer(s+b+" "+p);
  for(int i=1;i<11;i++)
   sb.append(i);
  String d = sb.toString();
  System.out.println(d);
  System.out.println();
  a = a + " is (not) " + m + "?";
  System.out.println(a);
}
}

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to