java StringBuilder、stringbuffer、string 源码阅读笔记

本文详细解析了StringBuilder、StringBuffer与String的内部实现原理及应用场景。对比了StringBuilder与StringBuffer的线程安全性,并阐述了String的不可变特性。

1、StringBuilder

Instances of <code>StringBuilder</code> are not safe for
 use by multiple threads. If such synchronization is required then it is
 recommended that {@link java.lang.StringBuffer} be used.可以看出stringbuilder不是线程安全的,stringbuffer是线程安全的

 

涉及的接口

//能够被添加 char 序列和值的对象。如果某个类的实例打算接收取自 Formatter 的格式化输出,那么该类必须实现 Appendable 接口

public interface Appendable {
    Appendable append(CharSequence csq) throws IOException;
    Appendable append(CharSequence csq, int start, int end) throws IOException;
    Appendable append(char c) throws IOException;
}

//CharSequence 是 char 值的一个可读序列。此接口对许多不同种类的 char 序列提供统一的只读访

public interface CharSequence {
    int length();
    char charAt(int index);
    CharSequence subSequence(int start, int end);
    public String toString();

}

 

抽象类

//一个可变的字符序列。

 

AbstractStringBuilder{//这里只贴一些部分重要的方法

   char value[];//从这可以看出 stringbuilder 使用了char数组类存储字符内容

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

   public void ensureCapacity(int minimumCapacity) {
     if (minimumCapacity > value.length) {
       expandCapacity(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);
    }

 

public abstract String toString();//抽象方法

}

 

public final class StringBuilder{//基本都调用了上面抽象类的方法append、delete、insert等

    public StringBuilder() {//默认初始化的数组长度为16
       super(16);
    }

 

public String toString() {//重写toString方法
        // Create a copy, don't share the array
 return new String(value, 0, count);
    }

private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException {//多了2个流写和读的方法
        s.defaultWriteObject();
        s.writeInt(count);
        s.writeObject(value);
    }

    /**
     * readObject is called to restore the state of the StringBuffer from
     * a stream.
     */
    private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
        s.defaultReadObject();
        count = s.readInt();
        value = (char[]) s.readObject();
    }

 

 

}

 

 

 

2、StringBuffer

继承的接口与类与StringBuilder一致,方法也基本一致,只是每个方法都加了synchronized关键字,来保证线程安全。代码就不贴了。

 

3、String

Strings are constant,their values cannot be changed after they
  are created

public final class String{

private final char value[];//string使用了一个final修饰的char数组,也就是不可变

 

 

public String concat(String str) {
 int otherLen = str.length();
 if (otherLen == 0) {
     return this;
 }
 char buf[] = new char[count + otherLen];
 getChars(0, count, buf, 0);
 str.getChars(0, otherLen, buf, count);
 return new String(0, count + otherLen, buf);
    }

 

public String[] split(String regex, int limit) {
 return Pattern.compile(regex).split(this, limit);
    }

}

 

代码就不贴了,常用的方法可以自己看下API。

 

 

 

 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值