2
private void doShareEmp(pageBean UTIL, HttpServletRequest request, String page)
        throws Exception
{
    doAction(request, UTIL, page);
    String action = pageBean.getSafeRequestOrNullParameter(request, "DO");
    long empRecNum = UTIL.getNumValue("EMPLOYEE", "REC_NUM");
    if (action != null)
    {
        if (action.startsWith("US:"))
            unshareEmployee(request, UTIL, action.substring(3));
        else if (action.equals("SHARE") && empRecNum != 0)
            shareEmployee(request, UTIL, empRecNum);
    }
    ListBean list = UTIL.getListBean(request, "EMPSHARELIST", true);
    if (empRecNum != 0)
    {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT FLDREC_NUM, FLDCOMPANY, FLDLOCATION, FLDDEPT FROM @SCHEMAEMPLVIEW WHERE FLDEMPLOYEE = ? AND FLDTABLE='SHARED' ORDER BY FLDCOMPANY, FLDLOCATION, FLDDEPT");
        ArrayList qryParms = new ArrayList();
        qryParms.add(new Long(empRecNum));
        list.setQuery(UTIL, sql, qryParms);
    }
    else
        list.init();
}

In this piece of code i am appending an query to a StringBuffer.

Which one will be better?

  1. String
  2. StringBuffer
  3. StringBuilder

6 Answers 6

12

StringBuilder is a replacement to StringBuffer in a single threaded environment since 1.5, so go with StringBuilder. If you are not going to do any other manipulation with the data after the fact, go with String.

Sign up to request clarification or add additional context in comments.

Comments

8

StringBuffer is only needed in threaded environment and if you need synchronization. Here it doesn't seem to be the case.

Also, your string seems defined one and for all, a simple String would be enough. A StringBuilder is interesting when you are modifying your "string" by appending content. If you already have all your content, no need for a StringBuilder.

But you can already read all these informations on their javadocs :

The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.

8 Comments

Isn't StringBuffer the thread-safe (syncronized) version and StringBuilder the non-synchronized version of the mutable CharSequence classes?
any major impact of using stringBuffer instead of String.
@John, yes, a lot of synchronization and performances lost (for nothing here). And a StringBuilder here would only be used to change an already existing string into a string, this isn't useful.
I have placed the complete method, from this can u say its right to use String instead of StringBuffer
@John, yes, your StringBuilder only appends one String, so no need of it. Simply use the String.
|
3

Strings are immutable in Java so any time you modify the String object you're using StringBuilder anyway. If the String is immutable then use String, otherwise create a StringBuilder and convert it to a String when you are done modifying it.

Comments

1

Unless you execute this method (to run the query) thousands of times per second, use a plain String. It's the most readable, fast and compact solution.

Comments

1

You might be too verbose. If your code is

String sql = "SELECT COLUMNA,";
if(foo)
    sql += "COLUMNB"
else
    sql += "COLUMNC"

Then the compiler is actually going to optimize and use a StringBuffer.

Comments

0

You should not be wasting your time worrying about how to concatenate 2 strings. Thats not the mark of a great programmer, if that's what you thought.

Try this ->

long finalTime1 = 0; {
long initialTimeTest = System.currentTimeMillis();
for( int index = 0; index < 10000; index++ ){ StringBuilder sb = new StringBuilder("Hello, ").append("World"); System.out.println(sb.toString()); }
finalTime1 = System.currentTimeMillis() - initialTimeTest;

}

long finalTime2 = 0; {
long initialTimeTest = System.currentTimeMillis();
for( int index = 0; index < 10000; index++ ){ String sb = "Hello, " + "World"; System.out.println( sb ); }
finalTime2 = System.currentTimeMillis() - initialTimeTest;
}

System.out.println( finalTime1 ); System.out.println( finalTime2 );

Results:

... Hello, World Hello, World 245 148

Did you think string buffer was faster ??

We are breaking the mother of all rules: Keep it Simple. -

For mundane string handling there is no reason why to use StringBuilder. It just adds unnecessary complexity to a mundane task.

Please, we need to think BIG, think in the overall business impact of the module to the project. Discussing whether we shall assemble two strings with StringBuilder or String is thinking little, - don't do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.