zoukankan      html  css  js  c++  java
  • JDK源码阅读--StringBuffer

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


    1 /**
    2      * A cache of the last value returned by toString. Cleared
    3      * whenever the StringBuffer is modified.
    4      */
    5     private transient char[] toStringCache;//被transient修饰,表示这个字段不被序列化



    StringBuffer是被final修饰的,表示不能被其它类继承,;继承了AbstractStringBuilder抽象类,实现了Serializable、CharSequence接口

    StringBuffer中的方法被public修饰的都使用了synchronized,保证线程安全。所以StringBuffer是线程安全的类。
     1 /**
     2      * Constructs a string buffer with no characters in it and an
     3      * initial capacity of 16 characters.
     4      */
     5     public StringBuffer() {
     6         super(16);
     7     }
     8 
     9     /**
    10      * Constructs a string buffer with no characters in it and
    11      * the specified initial capacity.
    12      *
    13      * @param      capacity  the initial capacity.
    14      * @exception  NegativeArraySizeException  if the {@code capacity}
    15      *               argument is less than {@code 0}.
    16      */
    17     public StringBuffer(int capacity) {
    18         super(capacity);
    19     }
    20 
    21     /**
    22      * Constructs a string buffer initialized to the contents of the
    23      * specified string. The initial capacity of the string buffer is
    24      * {@code 16} plus the length of the string argument.
    25      *
    26      * @param   str   the initial contents of the buffer.
    27      */
    28     public StringBuffer(String str) {
    29         super(str.length() + 16);
    30         append(str);
    31     }

    StringBuffer的容量默认是16,当容量不够的时候,容量会继续增加16,依次类推。

  • 相关阅读:
    VSCode拓展插件推荐(HTML、Node、Vue、React开发均适用)
    算法_栈的Java的通用数组实现
    算法_计算输入的算术表达式的值.
    设计模式整理_组合模式
    JavaSE复习_9 集合框架复习
    一个小题目的三种不同的解法
    设计模式整理_状态模式
    设计模式整理_迭代器模式
    设计模式整理_模板模式
    JavaSE复习_8 泛型程序设计
  • 原文地址:https://www.cnblogs.com/lixianyuan-org/p/10474759.html
Copyright © 2011-2022 走看看