String字符串性能优化的几种方案( 七 )

根据反编译结果 , 可以看到内部其实是通过StringBuilder进行字符串拼接的 。

再来执行例3的代码:

public class test2 {public static void main(String[
args) {        String s = \"\";        Random rand = new Random();for (int i = 0; i < 10; i++) {            s = s + rand.nextInt(1000) + \" \";                System.out.println(s);    

用反编译工具jad执行jad -o -a -s d.java test2.class进行反编译后 , 发现其内部同样是通过StringBuilder来进行拼接的:

推荐阅读