任何时候,我们都应该尝试首先阅读英文文档。 A thread-safe, mutable sequence of characters. 一个线程安全的,可改变的字符序列。 A string buffer is like a String, but can be modified.它很像String,但是它是可变的。 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.在任何时间点,它包含一个可改变的序列,通过方法的调用可以改变。 String buffers are safe for use by multiple threads字符串缓冲区对于多线程使用是安全的。 For example, if z refers to a string buffer object whose current contents are “start”, then the method call z.append(“le”) would cause the string buffer to contain “startle”, whereas z.insert(4, “le”) would alter the string buffer to contain “starlet”.使用append和insert方法可以添加字符串。 Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.每个缓冲区有一个特定的容量,当字符串太长时会自动扩展。 Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.除非另有说明,传递空参数会造成空指针异常。
StringBuffer都在这里,有需要的自行查看。
单线程:
public static void main(String[] args) { StringBuffer sb=new StringBuffer(); sb.append(true); System.out.println(sb.length()); sb.append(23.21); System.out.println(sb.length()); }输出分别为4,和9。true和23.21都变成了字符序列。 其他的应用也很简单,只要你想探索,对未知充满好奇。 多线程: 待续。。。
