StringBuffer与StringBuilder区别

来源:互联网 发布:邪恶吧下载软件 编辑:程序博客网 时间:2024/04/30 09:18
StringBuffer 字符串变量(线程安全) 
StringBuilder 字符串变量(非线程安全) 

String 类型和 StringBuffer 、StringBuilder 类型的主要性能区别其实在于 String 是不可变的对象,而后俩者都是可变的。 

来看看 StringBuffer类源码定义: 

Java代码  收藏代码
  1.    
  2. public final class StringBuffer  
  3.     extends AbstractStringBuilder  
  4.     implements java.io.Serializable, CharSequence  
  5. {  
  6.   
  7.     public StringBuffer() {  
  8.     super(16);  
  9.     }  
  10.   
  11.      public StringBuffer(int capacity) {  
  12.     super(capacity);  
  13.     }  


上例代码我们发现 StringBuffer 继承了 AbstractStringBuilder抽象类。包括构造方法的实现,都是父类提供的。 

然后,我们打开 StringBuilder源码定义: 

Java代码  收藏代码
  1. public final class StringBuilder  
  2.     extends AbstractStringBuilder  
  3.     implements java.io.Serializable, CharSequence  
  4. {  
  5.   
  6.     public StringBuilder() {  
  7.     super(16);  
  8.     }  
  9.   
  10.     public StringBuilder(int capacity) {  
  11.     super(capacity);  
  12.     }  

我们发现两者都继承了AbstractStringBuilder抽象类。且构造方法都调用父类实现。 

我们接着看两者append方法实现: 

先看StringBuffer的: 

Java代码  收藏代码
  1.     
  2. public synchronized StringBuffer append(Object obj) {  
  3.        super.append(String.valueOf(obj));  
  4.        return this;  
  5.    }  
  6.   
  7.    public synchronized StringBuffer append(String str) {  
  8.        super.append(str);  
  9.        return this;  
  10.    }  
  11.    //...  


再看StringBuilder的: 
Java代码  收藏代码
  1. public StringBuilder append(Object obj) {  
  2.     return append(String.valueOf(obj));  
  3. }  
  4.   
  5. public StringBuilder append(String str) {  
  6.     super.append(str);  
  7.     return this;  
  8. }  
  9. //...  


对比上面两段源码 我们发现 StirngBuffer 和StringBuilder的 append实现都是调用父类实现的。唯一不同的是 StringBuffer是线程安全的,方法中多了synchronized ,而StringBuilder 是非线程安全的。 

我们看下父类AbstractStringBuilder 定义 及 append 实现: 

Java代码  收藏代码
  1. abstract class AbstractStringBuilder implements Appendable, CharSequence {  
  2.     //底层与String类一样都是 char类型数组。  
  3.     char value[];  
  4.     //字符串长度  
  5.     int count;  
  6.   
  7.     AbstractStringBuilder() {  
  8.     }  
  9.   
  10.     AbstractStringBuilder(int capacity) {  
  11.         value = new char[capacity];  
  12.     }  


Java代码  收藏代码
  1. public AbstractStringBuilder append(Object obj) {  
  2.     return append(String.valueOf(obj));  
  3. }  
  4.   
  5. public AbstractStringBuilder append(String str) {  
  6.     //对于str==null的  
  7.     if (str == null) str = "null";  
  8.     int len = str.length();  
  9.     if (len == 0return this;  
  10.     //新的字符串长度  
  11.     int newCount = count + len;  
  12.     if (newCount > value.length)  
  13.          //当新的字符串长度比原先数组长度大时,需要对char 数组扩容。  
  14.            expandCapacity(newCount);  
  15.     //将新添的数据追加到char类型数组中。  
  16.      str.getChars(0, len, value, count);  
  17.     count = newCount;  
  18.     return this;  
  19. }  
  20.   
  21. //数组扩容   
  22. void expandCapacity(int minimumCapacity) {  
  23.     //先扩容成 (原先的长度+1)*2  
  24.     int newCapacity = (value.length + 1) * 2;  
  25.     //判断newCapacity值是否满足要求  
  26.      //如果新的长度还是不够,则直接取值 minimumCapacity   
  27.     if (newCapacity < 0) {  
  28.        newCapacity = Integer.MAX_VALUE;  
  29.     } else if (minimumCapacity > newCapacity) {  
  30.        newCapacity = minimumCapacity;  
  31.     }     
  32.     char newValue[] = new char[newCapacity];  
  33.     //将原先的数据拷贝到新的char 数组中。  
  34.      System.arraycopy(value, 0, newValue, 0, count);  
  35.     value = newValue;  
  36. }  
  37.   
  38.  public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {  
  39.     if (srcBegin < 0) {  
  40.         throw new StringIndexOutOfBoundsException(srcBegin);  
  41.     }  
  42.     if (srcEnd > count) {  
  43.         throw new StringIndexOutOfBoundsException(srcEnd);  
  44.     }  
  45.     if (srcBegin > srcEnd) {  
  46.         throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);  
  47.     }  
  48.     System.arraycopy(value, offset + srcBegin, dst, dstBegin,  
  49.          srcEnd - srcBegin);  
  50. }  
原创粉丝点击