What is the difference between String and StringBuffer ?, Stringstringbuffer
I often use String. To be honest, the number of StringBuffer operations is really small. The only method that makes me feel special is that the appand method is unique to StringBuffer, so what is the difference between them? We know that String is a String, and String is an object. The object is called a mutable object and a non-mutable object. String is a non-mutable object, stringBuffer is a variable object. Someone may ask String str = "aa"; str = "bb"; this sentence can also be compiled without any errors. Yes, this sentence does not have a slight error, but you ignore it, that is, "aa" and "bb" are strings, strings are objects, and they are not an object at all, string is an immutable object. In fact, String str = "aa" is actually a reference. It references an object, str points to a different address before it is equal to "aa", that is, it does not give the value to str, but references the object to him, that is The object address is given to him. However, it is impossible for you to change the data of the object address "aa" Through String, just like this program: the return value and input value of this program will not change.
Next we will talk about StringBuffer. This type can be modified, that is, it is modified on the original address. The difference lies in this.
Therefore, the conclusion is that if you operate the content in the String frequently, especially when the content is to be modified, use StringBuffer. If you need a String at the end, use the toString () of StringBuffer () good Way!
Maybe this is the performance bottleneck of your program!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
- A non-mutable object cannot be changed once it is created, and a mutable object can be changed after it is created.
- The String object is a non-mutable object, and the StringBuffer object is a mutable object.
- To achieve better performance, you need to carefully select one of the two based on your actual situation.