Java Tour (17)--stringbuffer Overview, store, delete, get, modify, invert, store buffer data into an array, StringBuilder
After we finish the string, let's talk about his little brother.
I. StringBuffer overview
What does buffer mean about the object of StringBuffer? The meaning of the buffer, string once initialized can not be changed, and StringBuffer is possible, which is the diffe
object, and it is separated from the string class.The most used is append and insert,append are inserted at the end of the string, insert is inserted into the specified position.3.StringBuilderBut not every time a lot of threading and frequent operations, but can not continue to create new objects with string impact performance.So there's a StringBuilder. APIs are compatible with StringBuffer, and are most suitable for single-threaded operations.Thre
Wrote a test and tried the Java StringBuilder class.The code is as follows:Import Java.util.Scanner;public class StringBuilder1{public static void Main (string[] args){Scanner in = new Scanner (system.in);System.out.println ("Start building a string with many small pieces of string:");System.out.println ("First build an empty string constructor");StringBuilder bu
StringBuffer class StringBuilder classString length size is not variableStringBuffer and StringBuilder length variableStringBuffer thread-Safe StringBuilder thread is unsafeFast StringBuilder SpeedObjects of the StringBuffer class StringBuilder class can be modified multip
* The string class is immutable, and any modification to a string causes the new object to be generated.
* StringBuffer and StringBuilder are mutable classes, and any changes to the string do not produce new objects.
When used in practice, if you often need to modify a string, such as inserting, deleting, etc.
* But what's the difference between StringBuffer and StringBuilder?
StringBuffer is thread-saf
1. Variable and immutabilityThe string is immutable, it is a character array that holds the value of the object and is declared as the final type, so the string object is a sequence of immutable characters. Private final char value[];Both StringBuffer and StringBuilder are variable sequences of characters, which are also saved by a character array. Char value[];2. Thread is secureString is final type, immutable, so it must be thread safe .String
synchronized StringBuffer reverse () { super.reverse (); return This ;} Public int indexOf (String str) { return0); }StringBuilder does not have a synchronous lock on the method, so it is non-thread safe.StringBuffer is suitable for multi-threaded programs, ensuring synchronization. The StringBuilder is suitable for single-threaded programs and does not guarantee synchronization.In mo
Java -- what is the difference between String, StringBuilder, and StringBuffer ?, Javastringbuffer
1. String is a read-only String, and the referenced String content cannot be changed.
2. The StringBuffer and StringBuilder string objects can be directly modified.
3. StringBuilder is not modified by the synchronized
Discovery also has a StringBuilder class, which is also a string buffer, what is the difference between StringBuilder and StringBuffer?We read the API description of StringBuilder and found that it is also a variable sequence of characters. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is desi
delete the contents of a string
The StringBuffer class is a container
Method is the same as String
Construction methodStringBuffer()//初始一个容量为16的字符串缓冲区StringBuffer(int size);//构造指定容量的字符串缓冲区StringBuffer(String str)//将内容初始化为指定字符串内容new StringBuffer("ttf");sb.append("love java");//给字符串缓冲区后面添加字符串Append can add integers or object typesStringBuilder
String Immutable character sequence
StringBuffer variable character sequences, low e
){ -Buffer.append ("KKKKKKKKKKKKKKKKKKKKKKKKKK" +k); toBuffer.append ("\ r \ n"); + } - Bw.write (buffer.tostring ()); the Bw.flush (); * //buffer = new StringBuffer (); $Buffer =NewStringBuilder ();Panax Notoginseng } - LongE=System.currenttimemillis (); theSystem.out.println (E-s); + bw.close (); A } the}Time line 24th outputs the result to 222~225.The results can be seen, the effect of optimization is very obvious, but it should be noted that:
1.String is a read-only string, and the referenced string content cannot be changed.The string objects of 2.StringBuffer and StringBuilder can be modified directly.3.StringBuilder is not modified by synchronized keyword, thread is unsafe, efficiency is higher than stringbuffer,stringbuffer thread safety.4. If the resulting string is already present in the static store, the "+" performance is better than the
the X to the left of the equals sign.4) The integer object is stored in memoryInteger m = 127;Integer n = 127;System.out.println (M = = n);//result is trueInteger a = 128;Integer B = 128;System.out.println (A = = B);//result is FalseThe reason for this difference is that when the value is within the byte range (shaping less than or equal to 127), for the new attribute, if the value already exists, no new space is created, that is, M and N point to the same object.Summarize1. String and StringBu
characters, with an initial capacity of 16Variable-length arraysThe data stored in the array, when not saved, will automatically open a new array, the new book Group is usually about 1 time times the length of the original array, and then copy the original array, and then continue to store new data, pay attention to efficiency issuesSecond, StringBuilder classActually, it's stringbuffer.The serious difference is that the birth time is different.Strin
string concatenation in Java, Stringbuilder,stringbuffer and +, in the end which is more appropriate? StringBuilder threads are unsafe, and the efficiency is higher than thread-safe stringbuffer. Before jdk1.5, the + operation produces a large number of string objects that affect the efficiency of the GC, but after jdk1.5, the use of the + operator does not neces
Strings are the most widely used data types in programming. They are simple and practical, but understanding them in depth will bring us more benefits.
String cache pool:
To save memory and improve resource reuse, Java introduces the String cache pool concept.
Strings in the cache Pool cannot be recycled:
Strings in the cache pool will not be recycled by the garbage collection mechanism. They are basically resident memory. Therefore, if you consum
, there are three places in the JVM where the same string value exists: the detention string in the constant table, the heap space of the Str1 object, and the heap space of the STR2 object. Therefore, the STR1==STR2 returned here is false. Call the string class to override the Equals () method to compare the values of two objects to return true.3. String "Object" created by the Intern () method: String str1= "string";String Str2=new string ("string"). Intern ();Intern (): When there is a string
String literal constantStringBuffer string variable (thread safe)StringBuilder string variable (non-thread safe)Briefly, the main performance difference between the string type and the StringBuffer type is that the string is an immutable object, so each time a change is made to the string type it is equivalent to generating a new string object and then pointing the pointer to the new Strin G object, so it is best not to use a string to change the cont
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service