java中StringBuilder、StringBuffer、String类之间的关系
2016-01-28 10:50
375 查看
http://www.jb51.net/article/33398.htm
String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间,StringBuffer是可变类,和线程安全的字符串操作类,任何对它指向的字符串的操作都不会产生新的对象,StringBuffer和StringBuilder类功能基本相似
1. String 类
String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间。
String a = "a"; //假设a指向地址0x0001
a = "b";//重新赋值后a指向地址0x0002,但0x0001地址中保存的"a"依旧存在,但已经不再是a所指向的,a 已经指向了其它地址。
因此String的操作都是改变赋值地址而不是改变值操作。
2. StringBuffer是可变类,和线程安全的字符串操作类,任何对它指向的字符串的操作都不会产生新的对象。 每个StringBuffer对象都有一定的缓冲区容量,当字符串大小没有超过容量时,不会分配新的容量,当字符串大小超过容量时,会自动增加容量。
StringBuffer buf=new StringBuffer(); //分配长16字节的字符缓冲区
StringBuffer buf=new StringBuffer(512); //分配长512字节的字符缓冲区
StringBuffer buf=new StringBuffer("this is a test")//在缓冲区中存放了字符串,并在后面预留了16字节的空缓冲区。
3.StringBuffer
StringBuffer和StringBuilder类功能基本相似,主要区别在于StringBuffer类的方法是多线程、安全的,而StringBuilder不是线程安全的,相比而言,StringBuilder类会略微快一点。对于经常要改变值的字符串应该使用StringBuffer和StringBuilder类。
4.线程安全
StringBuffer 线程安全
StringBuilder 线程不安全
5.速度
一般情况下,速度从快到慢:StringBuilder>StringBuffer>String,这种比较是相对的,不是绝对的。
6.总结
(1).如果要操作少量的数据用 = String
(2).单线程操作字符串缓冲区 下操作大量数据 = StringBuilder
(3).多线程操作字符串缓冲区 下操作大量数据 = StringBuffer
以下是代码与演示说明:
String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间,StringBuffer是可变类,和线程安全的字符串操作类,任何对它指向的字符串的操作都不会产生新的对象,StringBuffer和StringBuilder类功能基本相似
1. String 类
String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间。
String a = "a"; //假设a指向地址0x0001
a = "b";//重新赋值后a指向地址0x0002,但0x0001地址中保存的"a"依旧存在,但已经不再是a所指向的,a 已经指向了其它地址。
因此String的操作都是改变赋值地址而不是改变值操作。
2. StringBuffer是可变类,和线程安全的字符串操作类,任何对它指向的字符串的操作都不会产生新的对象。 每个StringBuffer对象都有一定的缓冲区容量,当字符串大小没有超过容量时,不会分配新的容量,当字符串大小超过容量时,会自动增加容量。
StringBuffer buf=new StringBuffer(); //分配长16字节的字符缓冲区
StringBuffer buf=new StringBuffer(512); //分配长512字节的字符缓冲区
StringBuffer buf=new StringBuffer("this is a test")//在缓冲区中存放了字符串,并在后面预留了16字节的空缓冲区。
3.StringBuffer
StringBuffer和StringBuilder类功能基本相似,主要区别在于StringBuffer类的方法是多线程、安全的,而StringBuilder不是线程安全的,相比而言,StringBuilder类会略微快一点。对于经常要改变值的字符串应该使用StringBuffer和StringBuilder类。
4.线程安全
StringBuffer 线程安全
StringBuilder 线程不安全
5.速度
一般情况下,速度从快到慢:StringBuilder>StringBuffer>String,这种比较是相对的,不是绝对的。
6.总结
(1).如果要操作少量的数据用 = String
(2).单线程操作字符串缓冲区 下操作大量数据 = StringBuilder
(3).多线程操作字符串缓冲区 下操作大量数据 = StringBuffer
以下是代码与演示说明:
public class TestCharacter { final static int time = 50000; //循环次数 public TestCharacter(){ } public void test(String s){ long begin = System.currentTimeMillis(); for(int i=0; i<time; i++){ s += “add”; } long over = System.currentTimeMillis(); System.out.println(“操作”+s.getClass().getName()+”类型使用的时间为:”+(over-begin)+”毫秒”); } public void test(StringBuffer s){ long begin = System.currentTimeMillis(); for(int i=0; i<time; i++){ s.append(“add”); } long over = System.currentTimeMillis(); System.out.println(“操作”+s.getClass().getCanonicalName()+”类型使用的时间为:”+(over-begin)+”毫秒”); } public void test(StringBuilder s){ long begin = System.currentTimeMillis(); for(int i=0; i<time; i++){ s.append(“add”); } long over = System.currentTimeMillis(); System.out.println(“操作”+s.getClass().getName()+”类型使用的时间为:”+(over-begin)+”毫秒”); } /*对 String 直接进行字符串拼接的测试*/ public void test2(){ String s2 = “abcd”; long begin = System.currentTimeMillis(); for(int i=0; i<time; i++){ String s = s2 + s2 +s2; } long over = System.currentTimeMillis(); System.out.println(“操作字符串对象引用相加类型使用的时间为:”+(over-begin)+”毫秒”); } public void test3(){ long begin = System.currentTimeMillis(); for(int i=0; i<time; i++){ String s =”abcd” + “abcd” + “abcd”; } long over = System.currentTimeMillis(); System.out.println(“操作字符串相加使用的时间为:”+(over-begin)+”毫秒”); } public static void main(String[] args){ String s1 = “abcd”; StringBuffer st1 = new StringBuffer(“abcd”); StringBuilder st2 = new StringBuilder(“abcd”); TestCharacter tc = new TestCharacter(); tc.test(s1); tc.test(st1); tc.test(st2); tc.test2(); tc.test3(); } }
相关文章推荐
- (有码)UIView常用方法
- HDU 1297:Children’s Queue
- UIColor+BeeExtension
- 百度UEditor(富文本编辑器)的基础用法
- UIView的layoutSubviews和drawRect方法何时调用
- 多层fragment嵌套时 部分fragment的UI无法显示
- The process of container handles the servlet request
- Caches 《ARM System Developer's Guide》chapter-12
- JS require 深入剖析
- UINavigationController 弹出新的UIViewController时,setNavigationBarHidden失效的问题
- UITableViewCell点击时背景颜色
- 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法
- Codeforces 609F Frogs and mosquitoes 二分+简单离散化线段树+Multimap
- 如何在SpriteBuilder中设置对象的通用属性
- 如何在SpriteBuilder中设置对象的通用属性
- 如何在SpriteBuilder中设置对象的通用属性
- ios9 xcode7 UIAlertController的常用用法
- UIView常见属性方法
- Win10 Redstone预览版本Build 11099/11102升级报错的解决方法
- win10 build 11102突变Build 14251到底怎么回事?