JDk1.8源码StringBuffer

一、概念

StringBuffer 
A thread-safe, mutable sequence of characters. A string buffer is like a {@link String}, but can be modified.

源码开头的一句话:线程安全的,可变字符顺序。一个类似String的字符串缓冲区,但是可以修改的!



 1  public final class StringBuffer
 2     extends AbstractStringBuilder
 3     implements java.io.Serializable, CharSequence
 4 {
 5 
 6     /**
 7      * A cache of the last value returned by toString. Cleared
 8      * whenever the StringBuffer is modified.
 9      */
10     private transient char[] toStringCache;
11 
12     /** use serialVersionUID from JDK 1.0.2 for interoperability */
13     static final long serialVersionUID = 3388685877147921107L;
14    
15     ..........
16 }

二、String定义成final

 StringBuffer类跟String类一样定义成final形式,主要是为了“效率”和“安全性”的考虑,若StringBuffer 被继承,由于它的高使用率,可能会降低它的性能。

它继承了AbstractStringBuilder类,

 1 abstract class AbstractStringBuilder implements Appendable, CharSequence {
 2     /**
 3      * The value is used for character storage.与String类一样,定义了一个char类型的数组存储值
 4      */
 5     char value[];
 6  
 7     /** 
 8      * The count is the number of characters used.
 9      */
10     int count;
11  
12     /** 
13      * This no-arg constructor is necessary for serialization of subclasses.
14      */
15     AbstractStringBuilder() {
16     }

而我们经常用到的是append方法,最常用的是 AbstractStringBuilder append(String str)

1  public AbstractStringBuilder append(String str) {
2         if (str == null)
3             return appendNull();//如果str==null,就会将null添加到尾部
4         int len = str.length();
5         ensureCapacityInternal(count + len);//比较重要的的一步
6         str.getChars(0, len, value, count);
7         count += len;
8         return this;
9     }
ensureCapacityInternal(int minimumCapacity),
确保容量至少等于指定的最小值。如果当前容量小于指定值,则创建新数组,新数组的容量为指定值的两倍加2;如果当前容量不小于指定值,则直接不做处理。
1  /**
2      * This method has the same contract as ensureCapacity, but is
3      * never synchronized.
4      */
5     private void ensureCapacityInternal(int minimumCapacity) {
6         // overflow-conscious code
7         if (minimumCapacity - value.length > 0)
8             expandCapacity(minimumCapacity);
9     }
expandCapacity(int minimumCapacity),
 1 void expandCapacity(int minimumCapacity) {
 2         int newCapacity = value.length * 2 + 2; //
  • 定义一个是原容量的2倍+2大小的值
     
 3         if (newCapacity - minimumCapacity < 0)
4 newCapacity = minimumCapacity;
5 if (newCapacity < 0) {
6 if (minimumCapacity < 0) // overflow
 7                 throw new OutOfMemoryError();
 8             newCapacity = Integer.MAX_VALUE;
 9         }
10         value = Arrays.copyOf(value, newCapacity); //扩容
11     }
 1  public static void main(String[] args) {
 2         StringBuffer sb = new StringBuffer();
 3 
 4         System.out.println("容量"+sb.capacity());
 5         sb.ensureCapacity(10);
 6         System.out.println("容量"+sb.capacity());
 7 
 8         sb.ensureCapacity(30);
 9         System.out.println("容量"+sb.capacity());//(16+1)*2=34>30
10         sb.ensureCapacity(80);
11         System.out.println("容量"+sb.capacity());//80>(34+1)*2=68
12         sb.ensureCapacity(90);
13         System.out.println("容量"+sb.capacity());//(80+1)*2>90
14 
15         //字符串返转
16         sb.append("abcde");
17         System.out.println(sb);
18         sb.reverse();
19 
20         System.out.println(sb);
21 
22     }

结果:扩容的大小与(之前的长度+1)*2相比较,那个大用哪个

1 容量16
2 容量16
3 容量34
4 容量80
5 容量162
6 abcde
7 edcba

----------------------------------------------------若有不正之处,欢迎大家指正,不胜感激!!!!!

原文地址:https://www.cnblogs.com/sqy123/p/9245414.html