千家信息网

AbstractStringBuilder类源码的示例分析

发表于:2025-11-15 作者:千家信息网编辑
千家信息网最后更新 2025年11月15日,这篇文章给大家分享的是有关AbstractStringBuilder类源码的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。因为看StringBuffer 和 Str
千家信息网最后更新 2025年11月15日AbstractStringBuilder类源码的示例分析

这篇文章给大家分享的是有关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类源码的示例分析"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

容量 方法 源码 数组 字符 构造器 字符串 此方法 缓冲区 缓冲 示例 分析 内容 大小 子类 实例 更多 篇文章 不错 实用 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 网络安全设计的原则有哪些 服务器远程管理端口未过滤 如何访问服务器 关于网络安全竞赛试题 世界手机版最好玩的服务器有哪些 网络安全小组组长副组长 国内软件开发公司排行榜 条形码管理软件开发 数据库设置年龄在18之前 搭建网络技术交流平台 请求服务器证书 广电网络安全生产管理 上海航恒网络技术有限公司 不信谣不传谣不造谣网络安全 互联网广告网络技术有限公司 网络安全全教程视频 苏州服务器市面价 计算机网络技术诞生的前提是 服务器自动生成新页面 百兆网络技术 文明重启怎么开个自己的服务器 教务系统数据库的修改 互联网属于科技类基金吗 测试连接不可识别的数据库格式 详细讲解sql数据库开发 阿里云服务器安全怎样保证 网络安全国际 聊城网络技术有限公司 手机玩家怎么创建mc服务器 滦南网络技术售后服务
0