Best practices/performance: mixing StringBuilder.append with String.concat

前端 未结 9 556
孤独总比滥情好
孤独总比滥情好 2020-12-07 08:23

I\'m trying to understand what the best practice is and why for concatenating string literals and variables for different cases. For instance, if I have code like this

9条回答
  •  情书的邮戳
    2020-12-07 08:55

    Optimization is done automatically by the compiler.

    The Java2 compiler will automatically convert the following:

    String s = s1 + s2; 
    

    to

    String s = (new StringBuffer()).append(s1).append(s2).toString();
    

    Taken straight from the Java Best Practices on Oracles website.

提交回复
热议问题