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 方法。

  • 相关阅读:
    工作中常用git命令总结
    工作中,实用map给数组去重的详解
    关于OC中的block自己的一些理解(一)
    存储过程专题(Oracle)
    ORACLE事物隔离级别和脏读、幻读、不可重复读区别
    C#客户端Json转DataTable
    C# Newtonsoft.Json JObject常用方法
    C#中的内部函数(子函数)
    C# Dev GridView当前行
    C#从数据库中加载照片的
  • 原文地址:https://www.cnblogs.com/kongieg/p/10628450.html
Copyright © 2011-2022 走看看