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.