Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String or StringBuffer

Tags:

java

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
like image 527
John Avatar asked Jul 03 '26 14:07

John


2 Answers

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.

like image 76
Aaron McIver Avatar answered Jul 05 '26 02:07

Aaron McIver


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.

like image 23
Colin Hebert Avatar answered Jul 05 '26 03:07

Colin Hebert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!