Easy tech stuff!

Here’s how you use StringBuilder

Posted: February 19th, 2008 | Author: Nischal Shetty | Filed under: Java | 2 Comments »

Now get out of that bad habit of using String objects to perform operations on strings. With StringBuilder(introduced in J2Se 5.0), you can perform those very append and other such operations more efficiently.

Scene 1: typical coder…. using the same old String object to perform append operations.

String s = “Hello”;

s = s + ” World”;

system.out.println(s);

I agree it’ll give you “Hello World”, but fella…. let’s be different from the rest and think about making the code more efficient.

Enter Scene 2.

StringBuilder sb = new StringBuilder(“Hello”);

sb.append(” World”);

system.out.println(sb);

well… thats it!! You’ll get your “Hello World” but in a more efficient way :)

http://www.techtamasha.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/google_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/twitter_48.png

2 Comments on “Here’s how you use StringBuilder”

  1. 1 omprakash said at 6:32 am on May 8th, 2010:

    Thanks,Its very much informative and easy to get.

  2. 2 Omiee said at 5:08 am on June 1st, 2010:

    good answer..thanks


Leave a Reply