详细解读AbstractStringBuilder类源码

 更新时间:2017年12月01日 10:51:23   作者:freedom_wei  
这篇文章主要介绍了详细解读AbstractStringBuilder类源码,具有一定参考价值,需要的朋友可以了解下。

因为看StringBuffer 和 StringBuilder 的源码时发现两者都继承了AbstractStringBuilder,并且很多方法都是直接super的父类AbstractStringBuilder的方法,所以还是决定先看AbstractStringBuilder的源码,然后再看StringBuffer 和 StringBuilder.

位置:java.lang包中

声明: abstract class AbstractStringBuilderimplements Appendable, CharSequence

AbstractStringBuilder 类有abstract 修饰,可知它不能被实例化。

AbstractStringBuilder 类有两个子类:StringBuilder和StringBuffer。

字段

 /**
     * The value is used for character storage.
     */
    char value[];
    /**
     * The count is the number of characters used.
     */
    int count;

构造器

1、无参构造器

AbstractStringBuilder() {
  }

2、创建abstractstringbuilder实现类的对象时指定缓冲区大小为capacity。

 AbstractStringBuilder(int capacity) {
    value = new char[capacity];
  }

当子类StringBuilder或StringBuffer实例化时,会在构造器中调用此构造器。

扩充容量

void expandCapacity(int minimumCapacity)

此方法有包访问权限,类中有多个方法会调用此方法,在容量不足时扩充容量。

源码:

 void expandCapacity(int minimumCapacity) {
    int newCapacity = (value.length + 1) * 2;
    if (newCapacity < 0) {
      newCapacity = Integer.MAX_VALUE;
    } else if (minimumCapacity > newCapacity) {
      newCapacity = minimumCapacity;
    }
    value = Arrays.copyOf(value, newCapacity);
  }

将缓冲区长度加1乘2的值赋予变量newCapacity, 然后将此值与指定的值比较,将较大值确定为缓冲区的新容量;然后调用Arrays类的copyof方法,此方法会创建一个新数组,然后将原数组中的字符全部复制进新数组中。

ensureCapacity(int minimumCapacity)

public void ensureCapacity(int minimumCapacity)

确保容量至少等于指定的最小值。如果当前容量小于指定值,则创建新数组,新数组的容量为指定值的两倍加2;如果当前容量不小于指定值,则直接不做处理。

源码:

 public void ensureCapacity(int minimumCapacity) {
    if (minimumCapacity > value.length) {
      expandCapacity(minimumCapacity);
    }
  }

测试:

    StringBuffer s = new StringBuffer();
    System.out.println("容量:" + s.capacity());// 容量:16
    s.ensureCapacity(10);
    System.out.println("容量:" + s.capacity());// 容量:16
    s.ensureCapacity(30);
    System.out.println("容量:" + s.capacity());// 容量:34
    s.ensureCapacity(80);
    System.out.println("容量:" + s.capacity());// 容量:80

方法

codePointAt方法中都是用Character.codePointAtImpl(value, index, count)来实现的

public int codePointAt(int index) {
    if ((index < 0) || (index >= count)) {
      throw new StringIndexOutOfBoundsException(index);
    }
    return Character.codePointAtImpl(value, index, count);
  }

getChars方法的实现用的是System.arraycopy()方法

public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
  {
    if (srcBegin < 0)
      throw new StringIndexOutOfBoundsException(srcBegin);
    if ((srcEnd < 0) || (srcEnd > count))
      throw new StringIndexOutOfBoundsException(srcEnd);
    if (srcBegin > srcEnd)
      throw new StringIndexOutOfBoundsException("srcBegin > srcEnd");
    System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
  }

append方法都牵扯到了ensureCapacityInternal()方法和getChars()方法来实现

public AbstractStringBuilder append(String str) {
    if (str == null)
      return appendNull();
    int len = str.length();
    ensureCapacityInternal(count + len);
    str.getChars(0, len, value, count);
    count += len;
    return this;
  }

使用了Arrays.copyOf()来实现

void expandCapacity(int minimumCapacity) {
    int newCapacity = value.length * 2 + 2;
    if (newCapacity - minimumCapacity < 0)
      newCapacity = minimumCapacity;
    if (newCapacity < 0) {
      if (minimumCapacity < 0) // overflow
        throw new OutOfMemoryError();
      newCapacity = Integer.MAX_VALUE;
    }
    value = Arrays.copyOf(value, newCapacity);
  }

Arrays.fill(value, count, newLength, ‘\0');字符串之间的复制

public void setLength(int newLength) {
    if (newLength < 0)
      throw new StringIndexOutOfBoundsException(newLength);
    ensureCapacityInternal(newLength);

    if (count < newLength) {
      Arrays.fill(value, count, newLength, '\0');
    }

    count = newLength;
  }

delete() 仅改变字符串的大小并未真正的删除字符串

public AbstractStringBuilder delete(int start, int end) {
    if (start < 0)
      throw new StringIndexOutOfBoundsException(start);
    if (end > count)
      end = count;
    if (start > end)
      throw new StringIndexOutOfBoundsException();
    int len = end - start;
    if (len > 0) {
      System.arraycopy(value, start+len, value, start, count-end);
      count -= len;
    }
    return this;
  }

学会灵活的运用System.arraycopy()方法

 public AbstractStringBuilder insert(int index, char[] str, int offset,
                    int len)
  {
    if ((index < 0) || (index > length()))
      throw new StringIndexOutOfBoundsException(index);
    if ((offset < 0) || (len < 0) || (offset > str.length - len))
      throw new StringIndexOutOfBoundsException(
        "offset " + offset + ", len " + len + ", str.length "
        + str.length);
    ensureCapacityInternal(count + len);
    System.arraycopy(value, index, value, index + len, count - index);
    System.arraycopy(str, offset, value, index, len);
    count += len;
    return this;
  }

总结

以上就是本文关于源码详细解读AbstractStringBuilder类源码详细解读的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

  • spring的TransactionSynchronizationAdapter事务源码解析

    spring的TransactionSynchronizationAdapter事务源码解析

    这篇文章主要介绍了spring的TransactionSynchronizationAdapter事务源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • 基于Java回顾之反射的使用分析

    基于Java回顾之反射的使用分析

    本篇文章是对Java反射的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • Java正则表达式API边界匹配

    Java正则表达式API边界匹配

    这篇文章主要介绍了Java正则表达式API边界匹配,文章围绕主题展开相应的相关资料,具有一定的参考价值,需要的朋友可以参考一下
    2022-06-06
  • SpringBoot框架底层原理解析

    SpringBoot框架底层原理解析

    这篇文章主要介绍了SpringBoot底层原理,包括配置优先级的配置方式给大家讲解的非常详细,需要的朋友可以参考下
    2024-03-03
  • Spring 使用Validation 验证框架的问题详解

    Spring 使用Validation 验证框架的问题详解

    Spring Boot在内部通过集成hibernate-validation已经实现了JSR-349验证规范接口,在Spring Boot项目中只要直接使用就行了。 一般用在Controller中用于验证前端传来的参数。这篇文章给大家介绍Spring Validation 验证框架的相关知识,感兴趣的朋友一起看看吧
    2021-07-07
  • JAVA 区分集合和数组

    JAVA 区分集合和数组

    这篇文章主要介绍了JAVA如何区分集合和数组,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • 基于SpringBoot多线程@Async的使用体验

    基于SpringBoot多线程@Async的使用体验

    这篇文章主要介绍了SpringBoot多线程@Async的使用体验,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 利用idea生成webservice客户端超详解步骤(wsdl文件的使用)

    利用idea生成webservice客户端超详解步骤(wsdl文件的使用)

    这篇文章主要给大家介绍了关于利用idea生成webservice客户端超详解步骤,第一次接触webservice,从采坑到采坑,算是了解了一些,明白了一些,文中通过代码以及图文介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • Android Home键监听的实现代码

    Android Home键监听的实现代码

    这篇文章主要介绍了Android Home 键监听的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • java中反射和注解的简单使用方法

    java中反射和注解的简单使用方法

    相信大家对注解和反射应该并不陌生,在现在信息飞速发展的年代,各种优秀的框架或许都离不开注解的使用,这篇文章主要给大家介绍了关于java中反射和注解的简单使用方法,需要的朋友可以参考下
    2021-08-08

最新评论