面试官请不要再问我StringBuffer与StringBuilder的区别了,源码讲你你听

StringBuffer类的定义——重点final,说明很多性质和String类似不可变
继承自AbstractStringBuilder

public final class StringBuffer
    extends AbstractStringBuilder
    implements java.io.Serializable, CharSequence
{……

无参构造函数,潜台词默认初始容量为16


    /**
     * Constructs a string buffer with no characters in it and an
     * initial capacity of 16 characters.
     */
    public StringBuffer() {
        super(16);
    }

指定大小的构造函数

 /**
     * Constructs a string buffer with no characters in it and
     * the specified initial capacity.
     *
     * @param      capacity  the initial capacity.
     * @exception  NegativeArraySizeException  if the {@code capacity}
     *               argument is less than {@code 0}.
     */
    public StringBuffer(int capacity) {
        super(capacity);
    }

super(capacity)的实现,底层是char数组

 /**
     * Creates an AbstractStringBuilder of the specified capacity.
     */
    AbstractStringBuilder(int capacity) {
        value = new char[capacity];
    }

指定字符串的构造函数,潜台词:容量为串的长度+16

  /**
     * Constructs a string buffer initialized to the contents of the
     * specified string. The initial capacity of the string buffer is
     * {@code 16} plus the length of the string argument.
     *
     * @param   str   the initial contents of the buffer.
     */
    public StringBuffer(String str) {
        super(str.length() + 16);
        append(str);
    }

append方法

 @Override
    public synchronized StringBuffer append(String str) {
        toStringCache = null;
        super.append(str);
        return this;
    }

重点synchronized:线程安全的,实际实现还是要看父类

父类的实现:

  /**
     * Appends the specified string to this character sequence.
     * <p>
     * The characters of the {@code String} argument are appended, in
     * order, increasing the length of this sequence by the length of the
     * argument. If {@code str} is {@code null}, then the four
     * characters {@code "null"} are appended.
     * <p>
     * Let <i>n</i> be the length of this character sequence just prior to
     * execution of the {@code append} method. Then the character at
     * index <i>k</i> in the new character sequence is equal to the character
     * at index <i>k</i> in the old character sequence, if <i>k</i> is less
     * than <i>n</i>; otherwise, it is equal to the character at index
     * <i>k-n</i> in the argument {@code str}.
     *
     * @param   str   a string.
     * @return  a reference to this object.
     */
    public AbstractStringBuilder append(String str) {
        if (str == null)
            return appendNull();
        int len = str.length();
        ensureCapacityInternal(count + len);
        str.getChars(0, len, value, count);// 从0开始,到当前数组长度,进行拷贝
        count += len;
        return this;
    }

分析ensureCapacityInternal(count + len);目的是为了确保容量至少等于指定的最小值


  /**
   * For positive values of {@code minimumCapacity}, this method
   * behaves like {@code ensureCapacity}, however it is never
   * synchronized.
   * If {@code minimumCapacity} is non positive due to numeric
   * overflow, this method throws {@code OutOfMemoryError}.
   */
  private void ensureCapacityInternal(int minimumCapacity) {
      // overflow-conscious code
      if (minimumCapacity - value.length > 0) {
          value = Arrays.copyOf(value,
                  newCapacity(minimumCapacity));
      }
  }

再看StringBuilder,几乎一样,不同处是append方法,没有采取同步措施


    @Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }

总结:StringBuilder/StringBuffer均继承自AbstractStringBuilder,大多底层方法均由AbstractStringBuilder实现,默认容量均为16
不同之处在于:StringBuffer线程安全,而StringBuilder线程不安全

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值