Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Servlets
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Servlets section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 3rd, 2007, 05:51 AM
Registered User
 
Join Date: Oct 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default String and StringBuffer

Hi .. can any just explain how they differ from each other..?

 
Old October 4th, 2007, 08:59 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

String is immutable. Which is a flashy word for "doesn't change". So, behind the scenes java makes a new String every time you change a String - which is slow.

If you're working with Strings that change a lot, using a StringBuffer will be a lot more efficient. Here's an example:

Code:
import java.util.Date;

public class tmp {
        public static void main(String argv[]) {
                int MAX = 10000;                      // careful with large values here!
                StringBuffer sb = new StringBuffer(); 
                String s = new String();

                long start = new Date().getTime();
                for (int i=0;i<MAX;i++) {
                        sb.append("xXxXx");
                }
                long end = new Date().getTime();
                System.out.println("StringBuffer takes " + (end-start) + " mS");
                start = new Date().getTime();
                for (int i=0;i<MAX;i++) {
                        s+="xXxXx";
                }
                end = new Date().getTime();
                System.out.println("String takes " + (end-start) + " mS");

        }
}

On one of my machines this gives:
Code:
maui:/home/charlie$ java tmp
StringBuffer takes 4 mS
String takes 7467 mS
maui:/home/charlie$
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
 
Old October 5th, 2007, 12:34 AM
Registered User
 
Join Date: Oct 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank u ciderpunx.

 
Old January 8th, 2008, 03:54 AM
jomet
Guest
 
Posts: n/a
Default

Hi,
very informative post..
the same is applicable for javascript also.

rather than just appending to a string
something like this will better

for (var i = 0; i < 10000; i++) {
stringBuffer.push("Tfg hsdgh");
}

regds

jomet.
---------------------------------------------
Once you start a working on something,
dont be afraid of failure and dont abandon it.
People who work sincerely are the happiest.





Similar Threads
Thread Thread Starter Forum Replies Last Post
StringBuffer ie vs. firefox pegasus51 BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 0 April 2nd, 2007 03:09 PM
StringBuffer - Appending a Substring Clarification nanap BOOK: Beginning Java 2, JDK 5 Edition 0 March 8th, 2006 08:28 AM
StringBuffer - Appending a Substring Clarification nanap BOOK: Beginning Java 2 0 March 8th, 2006 08:25 AM
how to replace a string with another string/number crmpicco Javascript How-To 4 March 14th, 2005 12:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.