DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Proper Use Of String And StringBuffer In Java

07.10.2006
| 56113 views |
  • submit to reddit
        // This isn't really code at all, this is a comment that I use 
// over and over again as I go through code written by people 
// who obviously don't understand when to use a String and when
// to use a StringBuffer.

// I put this block of text wherever I find problems with Strings
// and StringBuffers to improve understanding of how each one works
// and when to use one or the other.
//
// Rule #1: Use a StringBuffer if you intend to build a string out of
// dynamic elements. That is, if you are going to include the results
// of function calls or variables as part of the string then you use
// a StringBuffer and do append() calls. StringBuffer's append is
// much faster than using "+" to stick together String objects.
//
// Rule #2: But, if you are just building a string out of static pieces
// of text, it's better to use Strings and "+" than creating a
// StringBuffer and making append calls. Instead just use a String
// and a bunch of "+" signs between the sections. For example:
// String test = "this " + "is " + "a " + "test " + "string";
// is _not_ expensive. Why? Because all the pieces are static text and
// the compiler can make it into this _as it compiles the code_:
// String test = "this is a test string";
//
// Rule #3: For goodness sake, do this:
// StringBuffer test = new StringBuffer();
// test.append("static string ");
// test.append(dynamicCall());
// test.append(" another static string ");
// test.append(someVariable);
// DON'T DO THIS:
// StringBuffer test = new StringBuffer();
// test.append("static string " + dynamicCall());
// test.append(" another static string " + someVariable);
// This is officially the worst of all worlds. You need to use a
// StringBuffer in this case because you've got some dynamic parts
// but there is still addition going on between dynamic parts (the
// function call and variable) and static strings. Ack!
//
// If you can't remember anything else, remember this:
// static strings = String and "+"
// dynamic strings = StringBuffer and append() 
    

Comments

Alan Emke replied on Tue, 2009/06/09 - 11:36am

Dudde is a TROLL

Snippets Manager replied on Tue, 2009/03/03 - 12:05pm

LOL, cool. the string should buffer Israel real estate with those objects. Anybody believe it will work as well as Java string at AFI group?

Snippets Manager replied on Mon, 2008/12/15 - 10:09am

Or better yet... StringBuilder test = new StringBuilder( "static string " ).append( dynamicCall() ).append( "another static string ").append( someVariable ); Saves real estate :D Also, unless you are planning on needing synchronized "appending", StringBuilder is faster.

Snippets Manager replied on Tue, 2008/07/29 - 4:46pm

Note that you can compose the appends as it's a telescoped operator so instead of: StringBuffer test = new StringBuffer(); test.append("static string "); test.append(dynamicCall()); test.append(" another static string "); test.append(someVariable); you can do: StringBuffer test = new StringBuffer(); test.append("static string ").append(dynamicCall()).append(" another static string ").append(someVariable);