java String、StringBuffer、StringBuilder 的区别
StringBuffer、StringBuilder和String一样,也用来代表字符串。String类是不可变类,任何对String的改变都会引发新的String对象的生成;StringBuffer则是可变类,任何对它所指代的字符串的改变都不会产生新的对象。既然可变和不可变都有了,为何还有一个StringBuilder呢?相信初期的你,在进行append时,一般都会选择StringBuffer吧!
先说一下集合的故事,HashTable是线程安全的,很多方法都是 synchronized方法,而HashMap不是线程安全的,但其在单线程程序中的性能比HashTable要高。StringBuffer和 StringBuilder类的区别也是如此,他们的原理和操作基本相同,区别在于StringBufferd支持并发操作,线性安全的,适合多线程中使用。StringBuilder不支持并发操作,线性不安全的,不适合多线程中使用。新引入的StringBuilder类不是线程安全的, 但其在单线程中的性能比StringBuffer高。
所以它们的区别是:
String是字符串常量
StringBuffer是线程安全的字符串变量
StringBuilder是非线程安全字符串变量
在大部分情况下 StringBuffer > String,StringBuilder > StringBuffer。
这里说的是大部分情况下,有些情况下 String > StringBuffer 。以下的字符串对象生成中,String 效率是远要比 StringBuffer 快的:
String S1 = "This is only a" + " simple" + " test";
StringBuffer Sb = new StringBuilder("This is only a").append(" simple").append(" test");
因为在 JVM 里,这个
String S1 = "This is only a" + " simple" + "test";
其实就是:
String S1 = "This is only a simple test";
所以当然不需要太多的时间了。
下面是一段性能测试代码:
除了对多线程的支持不一样外,StringBuffer 和 StringBuilder 的使用几乎没有任何差别。
先说一下集合的故事,HashTable是线程安全的,很多方法都是 synchronized方法,而HashMap不是线程安全的,但其在单线程程序中的性能比HashTable要高。StringBuffer和 StringBuilder类的区别也是如此,他们的原理和操作基本相同,区别在于StringBufferd支持并发操作,线性安全的,适合多线程中使用。StringBuilder不支持并发操作,线性不安全的,不适合多线程中使用。新引入的StringBuilder类不是线程安全的, 但其在单线程中的性能比StringBuffer高。
所以它们的区别是:
String是字符串常量
StringBuffer是线程安全的字符串变量
StringBuilder是非线程安全字符串变量
在大部分情况下 StringBuffer > String,StringBuilder > StringBuffer。
这里说的是大部分情况下,有些情况下 String > StringBuffer 。以下的字符串对象生成中,String 效率是远要比 StringBuffer 快的:
String S1 = "This is only a" + " simple" + " test";
StringBuffer Sb = new StringBuilder("This is only a").append(" simple").append(" test");
因为在 JVM 里,这个
String S1 = "This is only a" + " simple" + "test";
其实就是:
String S1 = "This is only a simple test";
所以当然不需要太多的时间了。
下面是一段性能测试代码:
public class StringBuilderTest { private static final int count = 3000000; public static void stringTest() { long begin, end; begin = System.currentTimeMillis(); String test = new String(); for (int i = 0; i < count / 100; i++) { test = test + " add "; } end = System.currentTimeMillis(); System.out.println((end - begin) + " millis has elapsed when used String. "); } public static void stringBufferTest() { long begin, end; begin = System.currentTimeMillis(); StringBuffer test = new StringBuffer(); for (int i = 0; i < count; i++) { test = test.append(" add "); } end = System.currentTimeMillis(); System.out.println((end - begin) + " millis has elapsed when used StringBuffer. "); } public static void stringBuilderTest() { long begin, end; begin = System.currentTimeMillis(); StringBuilder test = new StringBuilder(); for (int i = 0; i < count; i++) { test = test.append(" add "); } end = System.currentTimeMillis(); System.out.println((end - begin) + " millis has elapsed when used StringBuilder. "); } public static void main(String[] args) { stringTest(); stringBufferTest(); stringBuilderTest(); } }结果:
1394 millis has elapsed when used String. 92 millis has elapsed when used StringBuffer. 43 millis has elapsed when used StringBuilder.从上面的结果来看,这三个类在单线程程序中的性能差别一目了然,采用String对象时,即使运行次数仅是采用其它对象的1/100(不用其它运行次数的1/100,在我机器上运行了两、三个小时才出结果,可见性能差别巨大),其执行时间仍然比其他对象高出10倍以上;而采用StringBuffer对象和采用StringBuilder对象的差别也比较明显。由此可见,如果我们的程序是在单线程下运行,或者是不必考虑到线程同步问题,我们应该优先使用StringBuilder类;当然,如果要保证线程安全,自然非StringBuffer莫属了。
除了对多线程的支持不一样外,StringBuffer 和 StringBuilder 的使用几乎没有任何差别。
0
赞
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦