简体   繁体   中英

Differences between these two sets of code (String x Stringbuffer)

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");

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.

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".

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM