首页 > 代码库 > JAVA中,字符串STRING与STRINGBUILDER的效率差异

JAVA中,字符串STRING与STRINGBUILDER的效率差异

如果可变字符串操作较多的话,用STRINGBUILDER显然优势得多。

public class HelloJava {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String str = "a";
        long starTime = System.currentTimeMillis();
        for(int i = 0; i<10000;i++){
            str = str + i;
        }
        long endTime = System.currentTimeMillis();
        long time = endTime - starTime;
        System.out.println("String time is " + time);
        System.out.println(str);
        StringBuilder builder = new StringBuilder("b");
        starTime = System.currentTimeMillis();
        for(int j = 0; j<10000;j++){
            builder = builder.append(j);
        }
        endTime = System.currentTimeMillis();
        time = endTime - starTime;
        System.out.println("StringBuilder time is " + time);
        System.out.println(builder);
        

    }

}