StringBuffer类【JDK源码分析】

简介: StringBuffer类【JDK源码分析】

前言


2022/10/24

路漫漫其修远兮,吾将上下而求索


本文是根据jdk学习所做笔记

仅供学习交流使用,转载注明出处

推荐

JDK API 1.6 中文版

说明

以下内容是结合很多资料进行编写的

源码为jdk1.8的

斜体样式 为自己的思考

下划线为自己所画的重点

StringBuffer类

基本信息

java.lang

类 StringBuffer


java.lang.Object

继承者 java.lang.StringBuffer


所有已实现的接口:

Serializable, Appendable, CharSequence


public final class StringBuffer

extends Object

implements Serializable, CharSequence


线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意时间点上它都包含某种特定的字符序列,但通过某些方法调用可以改变该序列的长度和内容。


可将字符串缓冲区安全地用于多个线程。可以在必要时对这些方法进行同步,因此任意特定实例上的所有操作就好像是以串行顺序发生的,该顺序与所涉及的每个线程进行的方法调用顺序一致。


StringBuffer 上的主要操作是 append 和 insert 方法,可重载这些方法,以接受任意类型的数据。每个方法都能有效地将给定的数据转换成字符串,然后将该字符串的字符追加或插入到字符串缓冲区中。append 方法始终将这些字符添加到缓冲区的末端;而 insert 方法则在指定的点添加字符。


例如,如果 z 引用一个当前内容为 “start” 的字符串缓冲区对象,则此方法调用 z.append(“le”) 会使字符串缓冲区包含 “startle”,而 z.insert(4, “le”) 将更改字符串缓冲区,使之包含 “starlet”。


通常,如果 sb 引用 StringBuilder 的一个实例,则 sb.append(x) 和 sb.insert(sb.length(), x) 具有相同的效果。


当发生与源序列有关的操作(如源序列中的追加或插入操作)时,该类只在执行此操作的字符串缓冲区上而不是在源上实现同步。


每个字符串缓冲区都有一定的容量。只要字符串缓冲区所包含的字符序列的长度没有超出此容量,就无需分配新的内部缓冲区数组。如果内部缓冲区溢出,则此容量自动增大。从 JDK 5 开始,为该类补充了一个单个线程使用的等价类,即 StringBuilder。与该类相比,通常应该优先使用 StringBuilder 类,因为它支持所有相同的操作,但由于它不执行同步,所以速度更快。


从以下版本开始:

JDK1.0

另请参见:

StringBuilder, String, 序列化表格

属性

    /**
     * A cache of the last value returned by toString. Cleared
     * whenever the StringBuffer is modified.
     */
    private transient char[] toStringCache;
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    static final long serialVersionUID = 3388685877147921107L;

构造方法



    /**
     * 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);
    }
    /**
     * 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);
    }
    /**
     * Constructs a string buffer that contains the same characters
     * as the specified {@code CharSequence}. The initial capacity of
     * the string buffer is {@code 16} plus the length of the
     * {@code CharSequence} argument.
     * <p>
     * If the length of the specified {@code CharSequence} is
     * less than or equal to zero, then an empty buffer of capacity
     * {@code 16} is returned.
     *
     * @param      seq   the sequence to copy.
     * @since 1.5
     */
    public StringBuffer(CharSequence seq) {
        this(seq.length() + 16);
        append(seq);
    }

部分方法

length

public int length()返回长度(字符数)。

指定者:

接口 CharSequence 中的 length

返回:

此对象表示的当前字符序列的长度。

  @Override
    public synchronized int length() {
        return count;
    }

capacity

public int capacity()返回当前容量。容量指可用于最新插入的字符的存储量,超过这一容量就需要再次进行分配。

返回:

当前容量

    @Override
    public synchronized int capacity() {
        return value.length;
    }

append

public StringBuffer append(char[] str)将 char 数组参数的字符串表示形式追加到此序列。

按顺序将数组参数中的字符追加到此序列的内容中。此字符将增加该参数的长度。


该方法的总体效果与以下操作过程的效果相同:先使用 String.valueOf(char[]) 方法将参数转换为字符串,然后将所得字符串的字符追加到此字符序列。


参数:

str - 要追加的字符。

返回:

此对象的一个引用。

insert

public StringBuffer insert(int offset,

char[] str)将 char 数组参数的字符串表示形式插入此序列中。

数组参数的字符将被插入此序列中 offset 所指示的位置处。此字符将增加该参数的长度。


该方法的最终效果与以下操作过程的效果相同:先使用 String.valueOf(char[]) 方法将参数转换为字符串,然后将所得字符串的字符插入到此字符序列中 offset 所指示的位置。


参数:

offset - 偏移量。

str - 一个字符数组。

返回:

此对象的一个引用。

抛出:

StringIndexOutOfBoundsException - 如果 offset 参数无效。

  @Override
    public synchronized StringBuffer insert(int offset, char[] str) {
        toStringCache = null;
        super.insert(offset, str);
        return this;
    }

AbstractStringBuilder

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

System

//Params:
//src – the source array.
//srcPos – starting position in the source array.
//dest – the destination array.
//destPos – starting position in the destination data.
//length – the number of array elements to be copied.
    public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

reverse

public StringBuffer reverse()将此字符序列用其反转形式取代。如果序列中存在代理项对 (surrogate pair),在 reverse 操作中将其作为单个字符处理。因此,高-低代理项的顺序不会反转。假设 n 为执行 reverse 方法前此字符序列的字符长度(并非 char 值的长度),则新字符序列中索引 k 处的字符将等于原字符序列索引 n-k-1 处的字符。

注意,进行 reverse 操作后,执行操作前未成对的低代理项和高代理项将成为代理项对。例如,反转 “\uDC00\uD800” 将生成有效的代理项对 “\uD800\uDC00”。


返回:

此对象的一个引用。

从以下版本开始:

JDK1.0.2

    /**
     * @since   JDK1.0.2
     */
    @Override
    public synchronized StringBuffer reverse() {
        toStringCache = null;
        super.reverse();
        return this;
    }

AbstractStringBuilder

public AbstractStringBuilder reverse() {
        boolean hasSurrogates = false;
        int n = count - 1;
        for (int j = (n-1) >> 1; j >= 0; j--) {//分成两半
            int k = n - j;
            char cj = value[j];//交换
            char ck = value[k];
            value[j] = ck;
            value[k] = cj;
            if (Character.isSurrogate(cj) ||
                Character.isSurrogate(ck)) {
                hasSurrogates = true;
            }
        }
        if (hasSurrogates) {
            reverseAllValidSurrogatePairs();
        }
        return this;
    }

toString

public String toString()返回此序列中数据的字符串表示形式。分配一个新的 String 对象,并将它初始化,以包含当前由此对象表示的字符串序列。然后返回此 String。对此序列的后续更改不影响该 String 的内容。


指定者:

接口 CharSequence 中的 toString

返回:

此字符序列的字符串表示形式。

@Override
    public synchronized String toString() {
        if (toStringCache == null) {
            toStringCache = Arrays.copyOfRange(value, 0, count);
        }
        return new String(toStringCache, true);
    }

AbstractStringBuilder

 public static char[] copyOfRange(char[] original, int from, int to) {
        int newLength = to - from;
        if (newLength < 0)
            throw new IllegalArgumentException(from + " > " + to);
        char[] copy = new char[newLength];
        System.arraycopy(original, from, copy, 0,
                         Math.min(original.length - from, newLength));
        return copy;
    }

另外

StringBuffer类和StringBuilder类

package com.day0217_1;
import org.junit.jupiter.api.Test;
/**
 * 关于StringBuffer和StringBuilder的使用
 */
public class StringBufferBuilderTest {
    /*
       String、StringBuffer、StringBuilder三者的异同?
       String:不可变的字符序列,底层使用char[]存储
       StringBuffer:可变的字符序列:线程安全的,效率低,底层使用char[]存储
       StringBuilder:可变的字符序列:jdk5.0新增的,线程不安全的,效率高,底层使用char[]存储
       源码分析:
       String str=new String();//new char[0];
       String str1=new String("abc")//new char[]{'a','b','c'};
       StringBuffer sb1=new StringBuffer();//new char[16];底层创建了一个长度为16的数组。
       System.out.println(sb1.length());//
       sb1.append('a');//value[0]='a';
       sb1.append('b');//value[1]='b';
       StringBuffer sb2=new StringBuffer();//char value =new char["abc".length()+16];
       //问题1.System.out.println(sb2.length());//3
       //问题2.扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层的数组。
                默认情况下,扩容为原来的2倍+2,同时将原有的数组中的元素复制到新的数组中。
                  指导意义:开发中建议大家使用:StringBuffer(int capacity)或StringBuilder(int capacity)
     */
    @Test
    public void test1(){
        StringBuffer sb1=new StringBuffer("abc");
        sb1.setCharAt(0,'m');
        System.out.println(sb1);
        StringBuffer sb2=new StringBuffer();
        System.out.println(sb2.length());//0
    }
}



总结

关键词:

  • 初始容量为16的char数组
  • 扩容问题:如果要添加的数据底层数组盛不下了,那就需要扩容底层的数组。
  • 默认情况下,扩容为原来的2倍+2,同时将原有的数组中的元素复制到新的数组中
  • 线程安全
  • append
  • resverse
  • toString

最后

开源=为爱发电

相关文章
|
6天前
|
Java 编译器 ice
【Java开发指南 | 第十五篇】Java Character 类、String 类
【Java开发指南 | 第十五篇】Java Character 类、String 类
25 1
|
6天前
|
索引 Python
模拟实现一个简单的string类
这个示例中,定义了一个简单的 `MyString`类,包含了常用的字符串操作,比如初始化、字符串拼接、长度获取、索引访问、大小写转换、相等判断等功能。请根据需求进行适当的修改和扩展。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
23 5
|
6天前
|
C语言 C++
【C++】string类(常用接口)
【C++】string类(常用接口)
21 1
|
5天前
|
C语言 C++ 容器
C++ string类
C++ string类
9 0
|
6天前
|
编译器 C++
【C++】继续学习 string类 吧
首先不得不说的是由于历史原因,string的接口多达130多个,简直冗杂… 所以学习过程中,我们只需要选取常用的,好用的来进行使用即可(有种垃圾堆里翻美食的感觉)
10 1
|
6天前
|
算法 安全 程序员
【C++】STL学习之旅——初识STL,认识string类
现在我正式开始学习STL,这让我期待好久了,一想到不用手撕链表,手搓堆栈,心里非常爽
17 0
|
6天前
|
存储 安全 测试技术
【C++】string学习 — 手搓string类项目
C++ 的 string 类是 C++ 标准库中提供的一个用于处理字符串的类。它在 C++ 的历史中扮演了重要的角色,为字符串处理提供了更加方便、高效的方法。
18 0
【C++】string学习 — 手搓string类项目
|
6天前
|
C++
【C++】string类(介绍、常用接口)
【C++】string类(介绍、常用接口)
18 2
|
6天前
|
存储 缓存 安全
【 Java中String源码分析(JVM视角你不来看看?】
【 Java中String源码分析(JVM视角你不来看看?】
16 0
|
6天前
|
存储 网络协议 Java
Java String类
Java String类
13 0