The difference (rpm) String, StringBuffer and StringBuilder of

            String 字符串常量(线程安全)StringBuffer 字符串变量(线程安全)StingBuilder字符串变量(非线程安全)
    简要的说, String 类型和 StringBuffer 类型的主要性能区别其实在于 String 是不可变的对象, 因此在每次对 String 类型进行改变的时候其实都等同于生成了一个新的 String 对象,然后将指针指向新的 String 对象,所以经常改变内容的字符串最好不要用 String ,因为每次生成对象都会对系统性能产生影响,特别当内存中无引用对象多了以后, JVM 的 GC 就会开始工作,那速度是一定会相当慢的。
    而如果是使用 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”);
 在这种情况下,生成 String S1 对象的速度会更快,而这个时候 StringBuffer 居然速度上根本一点都不占优势。其实这是 JVM 的一个把戏,在 JVM 眼里,这个

String S1 = “This is only a” + “ simple” + “test”; 其实就是: String S1 = “This is only a simple test”;

Of course, it does not require much time. But to note here is that if you are from another string String object, then speed is not so fast, such as:

String S2 = “This is only a”;
String S3 = “ simple”;
String S4 = “ test”;
String S1 = S2 +S3 + S4;
这时候 JVM 会规规矩矩的按照原来的方式去做,因此,在大部分情况下 **StringBuffer > String**

StringBuffer

Java.lang.StringBuffer thread-safe variable sequence of characters. String is similar to a string buffer, but can not be modified. While any point in time which contains a particular sequence of characters, but may be varied by a certain method calls the length and content of the sequence.
String buffer may be safely used in a plurality of threads. It may be necessary when these methods are synchronized so that all operations on any specific example is if the serial order of occurrence, and the method sequentially calls directed to each thread sequentially consistent.
The main operation of the insert and append is StringBuffer methods, can override these methods to receive any type of data. Each method can be effective in a given data into a string, then the character string is added or inserted into a string buffer. append method always adds these characters to the end of the buffer; the insert method is to add the character at the specified point.
For example, if the current content is a reference z "start" string buffer object, this method calls z.append ( "le") contains the string buffer will "startle", and z.insert (4, "le") would change the string buffer to contain "starlet".
Therefore, in most cases StringBuilder> StringBuffer

StringBuilder

java.lang.StringBuilder a variable sequence of characters is the new 5.0. This class provides a StringBuffer compatible with the API, but does not guarantee synchronization. The class is designed as a drop-in replacement StringBuffer, the string buffer is used when using a single thread (which is common). If possible, we recommend the use of such priority, because in most implementations, it is faster than StringBuffer. Both methods are substantially the same.
Generally speaking;
A: String content is immutable, and StringBuffer, StringBuilder contents are variable.
B: StringBuffer is synchronized, data security, low efficiency; the StringBuilder are not synchronized, data is insecure, high efficiency

Guess you like

Origin www.cnblogs.com/blackmlik/p/12076194.html