• JDK源码阅读--StringBuilder


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


    被final修饰,表示StringBuilder不能被其它类继承。
    继承了AbstractStringBuilder抽象类,实现了 Serializable、CharSequence接口。
    StringBuilder中的方法没有被Synchronized修饰,所以StringBuilder不是线程安全的。

     1  /**
     2      * Constructs a string builder with no characters in it and an
     3      * initial capacity of 16 characters.
     4      */
     5     public StringBuilder() {
     6         super(16);
     7     }
     8 
     9     /**
    10      * Constructs a string builder with no characters in it and an
    11      * initial capacity specified by the {@code capacity} argument.
    12      *
    13      * @param      capacity  the initial capacity.
    14      * @throws     NegativeArraySizeException  if the {@code capacity}
    15      *               argument is less than {@code 0}.
    16      */
    17     public StringBuilder(int capacity) {
    18         super(capacity);
    19     }
    20 
    21     /**
    22      * Constructs a string builder initialized to the contents of the
    23      * specified string. The initial capacity of the string builder is
    24      * {@code 16} plus the length of the string argument.
    25      *
    26      * @param   str   the initial contents of the buffer.
    27      */
    28     public StringBuilder(String str) {
    29         super(str.length() + 16);
    30         append(str);
    31     }

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

  • 相关阅读:
    20000+关注,开源两本硬核的原创电子书!
    Tail Latency学习
    Zabbix5.0 监控redis
    JAVA多线程(九) ForkJoin框架
    JAVA多线程(八) Condition源码分析
    程序员英语学习(二) 标点符号对应的英语单词汇总
    linux shell快速入门
    Ubuntu常用指令和快捷键汇总
    Win10常用快捷键汇总
    算法路漫漫(三) 荷兰国旗
  • 原文地址:https://www.cnblogs.com/lixianyuan-org/p/10474806.html
Copyright © 2020-2023  润新知