zoukankan      html  css  js  c++  java
  • StringBuffer and StringBuilder

    StringBuilder

    原文:

    public final class StringBuilder
    extends Object
    implements Serializable, CharSequence
    A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
    The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.
    For example, if z refers to a string builder object whose current contents are "start", then the method call z.append("le") would cause the string builder to contain "startle", whereas z.insert(4, "le") would alter the string builder to contain "starlet".
    In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x).
    Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.
    Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.
    Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.

    译:

    StringBuilder是在使用单线程时StringBuffer的替代品,StringBuilder没有同步的权限,再涉及多线程时,还是要继续使用StringBuffer。
    
    Method:方法主要有两个:
    StringBuilder sb = new StringBuild();
    1.sb.append(str);
    2.sb.insert(position,str);
    当参数为null时,使用append() 或insert() 会报空指针。

    stringbuffer

    原文:

    A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
    
    The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point.
    
    API Note:
    StringBuffer implements Comparable but does not override equals. Thus, the natural ordering of StringBuffer is inconsistent with equals. Care should be exercised if StringBuffer objects are used as keys in a SortedMap or elements in a SortedSet. See Comparable, SortedMap, or SortedSet for more information.


    与StringBuilder最大的区别是它是stringbuffer是线程安全的.

    主要方法与stringbuilder相同,参数未null 时会报空指针

    Note:StringBuffer虽然实现了Comparable 接口,但它没有重写equals 方法。

  • 相关阅读:
    zabbix 4.0 监控磁盘IO的实施笔记
    梅登黑德定位系统
    sdrplay sdr 支持的sample rate
    记录一下几个中移动可以PING的检测地址及部份DNS设置
    升级mariadb 10后目录权限问题的笔记
    C#单独启动进程的几种方式及使用特点(使用不当导致端口无法释放)
    SqlBulkCopy批量插入数据时,不执行触发器和约束的解决方法
    C# 处理大量数据的技巧
    C# 几种集合性能比较
    WPF学习网址整理
  • 原文地址:https://www.cnblogs.com/kongieg/p/10628450.html
Copyright © 2011-2022 走看看