public class test { public static void main(String[] args) { //System.getProperties(); String str = ""; StringBuilder str2 = new StringBuilder(); long start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { System.out.print(i+"\r"); str += "neko"; } long end = System.currentTimeMillis(); System.out.println(end - start); start = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { str2.append("neko"); } end = System.currentTimeMillis(); System.out.println(end - start); } }
可以看到 String会不断创建新的对象,速度不算快 StringBuilder速度很快 打开Bytecode也是一样的 底层的+依然是StringBuilder实现的