String、StringBuilder、StringBuffer对比
参考:http://swiftlet.net/archives/1694
http://www.cnblogs.com/springcsc/archive/2009/12/03/1616326.html
String 字符串(character strings),不是线程安全的
StringBuilder 非线程安全的字符串序列(A mutable sequence of characters)
StringBuffer 线程安全的字符串序列(A thread-safe, mutable sequence of characters)
1、初始化方式
String
String str="abc"; String str1=new String("abc");
这两种初始化的方式的差别是:后者产生了两个对象,内存浪费。
StringBuffer
StringBuffer sb1=new StringBuffer("abc"); StringBuffer sbf1=new StringBuffer(); sbf1.append("abc");
从源码上没看出来这两种的明显差异。有老司机明白的可以指点一二。
StringBuilder
和StringBuffer的初始化方式是一样的。
2、性能比较
测试代码
package cn.simple.src_study; public class StringStudy { public static void main(String[] args) { long strStart = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { String str = "abc"; } long strEnd = System.currentTimeMillis(); System.out.println("str=\"abc\"; 执行10000次耗时:" + (strEnd - strStart)); long strStart1 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { String str1 = new String("abc"); } long strEnd1 = System.currentTimeMillis(); System.out.println("str=new String(\"abc\"); 执行10000次耗时:" + (strEnd1 - strStart1)); long sbfStart = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { StringBuffer sbf = new StringBuffer("abc"); } long sbfEnd = System.currentTimeMillis(); System.out.println("sbf=new StringBuffer(\"abc\"); 执行10000次耗时:" + (sbfEnd - sbfStart)); long sbfStart1 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { StringBuffer sbf1 = new StringBuffer(); sbf1.append("abc"); } long sbfEnd1 = System.currentTimeMillis(); System.out.println("sbf.append(\"abc\"); 执行10000次耗时:" + (sbfEnd1 - sbfStart1)); long sbdStart = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { StringBuilder sbd = new StringBuilder("abc"); } long sbdEnd = System.currentTimeMillis(); System.out.println("sbd=new StringBuilder(\"abc\"); 执行10000次耗时:" + (sbdEnd - sbdStart)); long sbdStart1 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { StringBuilder sbd = new StringBuilder(); sbd.append("abc"); } long sbdEnd1 = System.currentTimeMillis(); System.out.println("sbd.append(\"abc\"); 执行10000次耗时:" + (sbdEnd1 - sbdStart1)); } }
结果截图
从测试结果可以看出:如果值是固定的直接赋值给字符串,效率最高。
如果字符串存在拼接过程呢?
测试代码:
package cn.simple.src_study; public class StringStudy2 { public static void main(String[] args) { long strStart = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { String str = "abc"; str+="def"; } long strEnd = System.currentTimeMillis(); System.out.println("str=\"abc\"; 执行10000次耗时:" + (strEnd - strStart)); long strStart1 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { String str1 = new String("abc"); str1+="edf"; } long strEnd1 = System.currentTimeMillis(); System.out.println("str=new String(\"abc\"); 执行10000次耗时:" + (strEnd1 - strStart1)); long sbfStart = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { StringBuffer sbf = new StringBuffer("abc"); sbf.append("edf"); } long sbfEnd = System.currentTimeMillis(); System.out.println("sbf=new StringBuffer(\"abc\"); 执行10000次耗时:" + (sbfEnd - sbfStart)); long sbfStart1 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { StringBuffer sbf1 = new StringBuffer(); sbf1.append("abc"); sbf1.append("edf"); } long sbfEnd1 = System.currentTimeMillis(); System.out.println("sbf.append(\"abc\"); 执行10000次耗时:" + (sbfEnd1 - sbfStart1)); long sbdStart = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { StringBuilder sbd = new StringBuilder("abc"); sbd.append("edf"); } long sbdEnd = System.currentTimeMillis(); System.out.println("sbd=new StringBuilder(\"abc\"); 执行10000次耗时:" + (sbdEnd - sbdStart)); long sbdStart1 = System.currentTimeMillis(); for (int i = 0; i < 10000; i++) { StringBuilder sbd = new StringBuilder(); sbd.append("abc"); sbd.append("edf"); } long sbdEnd1 = System.currentTimeMillis(); System.out.println("sbd.append(\"abc\"); 执行10000次耗时:" + (sbdEnd1 - sbdStart1)); } }
测试结果:
从测试结果可以看出:存在字符串拼接的情况下性能StringBuilder>StringBuffer>String