user892871
user892871

Reputation: 1025

Difference between String and StringBuilder and their internal organization

This is a very basic question. The extent of the answer i know to it is that Strings are immutable. Stringbuilders are not, so you can append characters at the end.

So how are stringbuilders internally organized?? String is an array of characters.

Is StringBuilder an array of characters too? So, I have a StringBuilder MY_OBJ= "Hello". Now if i try to append characters to the end of MY_OBJ, does it not mean that you are actually creating a new array object and copying all these chars into a new one? If so how is it more efficient than a string?

And another question I have in mind is, how does one mark the end of a StringBuilder? Like in C, we use a "/0"

Upvotes: 7

Views: 7918

Answers (3)

buritos
buritos

Reputation: 598

StringBuilder is not more efficient than String if you are not planning to change the value. It is more efficient if you want to append / remove characters to / from a string.

StringBuilder's default constructor will create an array which can hold 16 char elements. #append(String) copies the characters of the string to the StringBuidler's array using String#getChars(int, int, char[], int). The array is re-sized using Arrays.copyOf(char[], int) only when there is no space to add more characters. Each time this happens the capacity of the array is doubled. The end is marked by counting how many characters are in the array.

In old versions of Java or when you are concatenating strings inside a loop, each string concatenation with the + operator creates a new String object. StringBuilder is faster because it creates less objects.

If you are going to hold on to a StringBuilder instance you may want to call StringBuilder#trimToSize() after you are done with the modifications. It will attempt to reduce the capacity of the array to the minimum required.

Hope this helps.

Upvotes: 2

Maurício Linhares
Maurício Linhares

Reputation: 40333

Most of the StringBuilder implementation comes from AbstractStringBuilder and in Sun's implementation it's a wrapper around a char array. There is no marking of the end of the string, the class itself mantains a count variable that says how big the string really is. There is also a capacity method that's going to tell you how big the real array is (and a trimToSize method to trim the string builder to an array that is as big as the current stored string).

New arrays are only created when you reach the current string builder capacity and this operation is not that expensive as it uses the arrayCopy method and the capacity always doubles, so as you grow it becomes less likely that you are going to reach the limit. If you know beforehand the size you think the string builder is going to be you can also define on it's constructor so it does not have to copy the contents to the new array.

Also, this code:

StringBuilder MY_OBJ= "Hello";

Does not work.

But this one does:

StringBuilder MY_OBJ= new StringBuilder("Hello");

Upvotes: 6

Bert F
Bert F

Reputation: 87533

I dunno. Let's go see:

72   public final class StringBuilder
73       extends AbstractStringBuilder
45        * The value is used for character storage.
46        */
47       char[] value;
48   
49       /**
50        * The count is the number of characters used.
51        */
52       int count;

===

Is StringBuilder an array of characters too?

Apparently, in this particular implementation.

So, I have a StringBuilder MY_OBJ= "Hello". Now if i try to append characters to the end of MY_OBJ, does it not mean that you are actually creating a new array object and copying all these chars into a new one?

Not necessarily. The array isn't necessarily full (count < value.length), so a new array may not need to be allocated. Ideally, you initialized StringBuilder a capacity so that large enough array was allocated from the start.

StringBuilder sb = new StringBuilder(20);
sb.append("Hello");
...
sb.append(" there");

And another question I have in mind is, how does one mark the end of a StringBuilder? Like in C, we use a "/0"

You don't care - String/StringBuilder will handle it internally.

Upvotes: 12

Related Questions