Java笔记—String,StringBuffer ,StringBuilder 的区别

来源:互联网 发布:网络词强撩是什么意思 编辑:程序博客网 时间:2024/06/05 14:28

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. String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

     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. 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".

    In general, if sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

    Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.

    Every string buffer has a capacity. As long as the length of the character sequencecontained 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. As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBulider. The StringBuilder class should generally be used in preference to this one, as it support all of the same operations but it is faster, as itperforms no synchronization.

StringBuilder

A mutable sequence of characters. This class provides an API compatible withStringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement forStringBuffer 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 toStringBuffer as it will be faster under most implementations.

The principal operations on aStringBuilder are theappend 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. Theappend method always adds these characters at the end of the builder; theinsert method adds the characters at a specified point.

For example, ifz refers to a string builder object whose current contents are "start", then the method callz.append("le") would cause the string builder to contain "startle", whereasz.insert(4, "le") would alter the string builder to contain "starlet".

In general, if sb refers to an instance of aStringBuilder, thensb.append(x) has the same effect assb.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 ofStringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended thatStringBuffer be used.

String 对象是不可变的对象, 因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然后将指针指向新的 String 对象,所以经常改变内容的字符串最好不要用 String ,因为每次生成对象都会对系统性能产生影响。而StringBuffer 类每次都会对 StringBuffer 对象本身进行操作,而不是生成新的对象,再改变对象引用。所以在一般情况下我们推荐使用 StringBuffer ,特别是字符串对象经常改变的情况下。而在某些特别情况下, String 对象的字符串拼接其实是被 JVM 解释成了 StringBuffer 对象的拼接,所以这些时候 String 对象的速度并不会比 StringBuffer 对象慢,而特别是以下的字符串对象生成中, String 效率是远要比 StringBuffer 快的:
String S1 = “This is only a” + “ simple” + “ test”;
StringBuffer Sb = new StringBuilder(“This is only a”).append(“ simple”).append(“ test”);

这个时候 StringBuffer 在速度上一点都不占优势。在 JVM 眼里,这个String S1 = “This is only a” + “ simple” + “test”; 其实就是:String S1 = “This is only a simple test”; 当然不需要太多的时间了。但大家这里要注意的是,如果字符串是来自另外的 String 对象的话,速度就没那么快了,譬如:
String S2 = “This is only a”;
String S3 = “ simple”;
String S4 = “ test”;
String S1 = S2 +S3 + S4;
这时候 JVM 会规规矩矩的按照原来的方式去做。

原创粉丝点击