0

Our project is near deployment and some points were passed to us. One of them is to exchange string concatenation to stringbuffers.

But some of our strings are SQL queries and they are quite large. When i pass those strings as the parameters of the Stringbuffer, the concatenation still happens. So, theres any difference between these two sets of code?

Without stringbuffer

private static final String QUERY_CONSULTA_CIDADE_FUVIAL = "SELECT SR.ID_SUB_REGIAO FROM REGIAO_TAB_NEGC RT "
            + "INNER JOIN SUB_REGIAO_TAB_NEGC ST ON ST.ID_REGIAO_TAB_NEGC = RT.ID_REGIAO_TAB_NEGC "
            + "INNER JOIN SUB_REGIAO SR ON SR.ID_SUB_REGIAO = ST.ID_SUB_REGIAO INNER JOIN CIDADE C1 ON C1.ID_ESTADO = SR.ID_UF "
            + "WHERE RT.ID_TAB_NEGC = :idTabelaNegociacao AND C1.ID_CIDD = :idCidade AND SR.FL_FLUVIAL = 'S' AND C1.TP_REDSP_FLUV = 'S'";

With stringbuffer

private static final StringBuffer QUERY_CONSULTA_CIDADE_PERTENCE_SUB_REGIAO = new StringBuffer(
            "SELECT SR.ID_SUB_REGIAO FROM REGIAO_TAB_NEGC RT "
                    + "INNER JOIN SUB_REGIAO_TAB_NEGC ST ON ST.ID_REGIAO_TAB_NEGC = RT.ID_REGIAO_TAB_NEGC "
                    + "INNER JOIN SUB_REGIAO SR ON SR.ID_SUB_REGIAO = ST.ID_SUB_REGIAO "
                    + "INNER JOIN CIDADE C1 ON C1.ID_ESTADO = SR.ID_UF AND C1.TP_CLASS_COMRC_RODO = SR.TP_CLASF "
                    + "WHERE RT.ID_TAB_NEGC = :idTabelaNegociacao AND C1.ID_CIDD = :idCidade");
2
  • A StringBuilder (why Buffer?) doesn't make sense if you pass in a single String (and don't plan changes or additions.) Commented Feb 28, 2015 at 13:12
  • 2
    If there is all String literals around the +, the compiler should do the concatenation at compile time. Commented Feb 28, 2015 at 13:14

3 Answers 3

2

StringBuffer would be used as follows:

 StringBuffer str = new StringBuffer ("Stanford ");
 str.append("Lost!!");

for a "private static final String" you can't really use a StringBuffer or at least there isn't any performance gain, or possibly even performance loss!

StringBuffer is for use inside methods.

Also, as your variable is a private static final, all the '+' modifiers will happen at compile time and will be optimised anyway so you will be highly optimised. No need to change anything here.

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

1 Comment

There's no reason you couldn't use a StringBuffer to initialize a private static final field field, the syntax is just less convenient. (You need a static initializer block)
1

String and StringBuffer and StringBuilder are inbuild classes of Java. The basic difference here is that StringBuffer and StringBuilder are mutable and while String class is immutable. For ex:

String s="abc";
StringBuffer sb=new StringBuffer("test");

(both here s and sb creates a refernce to a string object of "abc" and "test" in heap and s and sb are just reference to that value which is stored in heap while these refernce variable are stored in stack). So now any changes made in s will never change the original value which string s is refering( since string classes are immutable) while sb can be. for ex:

->sb.append(" 123");

->"test 123"

now the value to which sb will be referring becomes "test 123".

->s2=s.append(" 123");
->s2="abc 123"

where s will still refer to same "abc".

Comments

0

Well, the concatenations in StringBuffer are faster than concatenating individual Strings. Since you're talking about production environment, you might need to consider whether to use StringBuffer or StringBuilder. StringBuffer is thread-safe, and hence slow as compared to StringBuilder.

You can check the differences between String and StringBuffer here: What is the difference between String and StringBuffer in Java?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.