ok please help me to understand this two things...."String" and "StringBuilder". Important point is, both are Object. In addition to that StringBuilder is not synchronized means not thread safe. By the way, the only major thing (code wise) which differenciate these two are as follows:
For String (creating 2 reference objects, 1 reference variable):
Note: Although JVM will create an anonymous object with value "abcdef" at line number2, but that will distroy immediately (but will remain in the memory). And therefore in line number 3 we will see the output as "abc"
For StringBuilder (creating 1 reference object, 1 reference variable) <---- I am not sure:
Note: Here, JVM will hold the memory and will modify again and again the same String object
That's how StringBuilder bits the String object. Am i right? If there is some thing more important points are missing here then please add.
On the other hand, I know that String object is immutable. But here are not we changing it's value while using StringBuilder class? Or again here that instance of Object concept is leading the floor? Can any one please help me to make this concept clear?
and you can't change it. The reason String objects are immutable is related to the String constant pool, it makes Java more memory efficient when two references points to the same String. In my opinion StringBuilder is a whole different story, it acts as a buffer with a fixed capacity, once that buffer overflows, it is automatically made larger.
Both are useful , Strings for normal character manipulation and StringBuilder for file I/O streams or large blocks of changing characters.
Randy Hernandez welcome to JavaRanch!!!
You are right Randy Hernandez.
StringBuilder: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.
String:The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created.
StringBuffer is thread-safe. That comes at a cost, and in most cases where people use StringBuffer/StringBuilder, they don't actually need it to be thread safe (how often you you have more than one thread writing to the same StringBuffer?). If you don't need the thread-safety, use a StringBuilder as it will be slightly faster.
Joined: Jul 09, 2001
Thank you Matthew.
So, this is interesting, the relation, let's say of a certain Java program and the underlying threads. Ok, so I have a program that processes data via a StringBuffer or StringBuilder. How many threads would a program like this use?
Joined: Jul 09, 2001
In other words, how can I be sure that my block of code that uses StringBuilder is indeed thread-safe, if the code doesn't contain any synchronized statements, either explicitly or implicitly (as in StringBuffer)?