Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

like image 578
user892871 Avatar asked Sep 01 '11 03:09

user892871


2 Answers

I dunno. Let's go see:

  • http://www.docjar.com/html/api/java/lang/StringBuilder.java.html
72   public final class StringBuilder
73       extends AbstractStringBuilder
  • http://www.docjar.com/html/api/java/lang/AbstractStringBuilder.java.html
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.

like image 106
Bert F Avatar answered Sep 19 '22 21:09

Bert F


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");
like image 26
Maurício Linhares Avatar answered Sep 21 '22 21:09

Maurício Linhares