Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the StringBuilder class is implemented? Does it internally create new string objects each time we append?

How the StringBuilder class is implemented? Does it internally create new string objects each time we append?

like image 859
Ram Avatar asked Aug 25 '10 10:08

Ram


People also ask

Does StringBuilder create new string?

StringBuilder will only create a new string when toString() is called on it. Until then, it keeps an char[] array of all the elements added to it. Any operation you perform, like insert or reverse is performed on that array.

How does StringBuilder works internally in Java?

StringBuilder objects are like String objects, except that they can be modified. Internally, these objects are treated like variable-length arrays that contain a sequence of characters. At any point, the length and content of the sequence can be changed through method invocations.

How does StringBuilder append work?

append(String str) method appends the specified string to this character sequence. The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument.

What is append in StringBuilder append?

StringBuilder. append(boolean a) is an inbuilt method in Java which is used to append the string representation of the boolean argument to a given sequence. Syntax : public StringBuilder append(boolean a) Parameter: This method accepts a single parameter a of boolean type and refers to the Boolean value to be appended.


2 Answers

In .NET 2.0 it uses the String class internally. String is only immutable outside of the System namespace, so StringBuilder can do that.

In .NET 4.0 String was changed to use char[].

In 2.0 StringBuilder looked like this

public sealed class StringBuilder : ISerializable {     // Fields     private const string CapacityField = "Capacity";     internal const int DefaultCapacity = 0x10;     internal IntPtr m_currentThread;     internal int m_MaxCapacity;     internal volatile string m_StringValue; // HERE ----------------------     private const string MaxCapacityField = "m_MaxCapacity";     private const string StringValueField = "m_StringValue";     private const string ThreadIDField = "m_currentThread"; 

But in 4.0 it looks like this:

public sealed class StringBuilder : ISerializable {     // Fields     private const string CapacityField = "Capacity";     internal const int DefaultCapacity = 0x10;     internal char[] m_ChunkChars; // HERE --------------------------------     internal int m_ChunkLength;     internal int m_ChunkOffset;     internal StringBuilder m_ChunkPrevious;     internal int m_MaxCapacity;     private const string MaxCapacityField = "m_MaxCapacity";     internal const int MaxChunkSize = 0x1f40;     private const string StringValueField = "m_StringValue";     private const string ThreadIDField = "m_currentThread"; 

So evidently it was changed from using a string to using a char[].

EDIT: Updated answer to reflect changes in .NET 4 (that I only just discovered).

like image 160
Brian Rasmussen Avatar answered Sep 24 '22 01:09

Brian Rasmussen


The accepted answer misses the mark by a mile. The significant change to StringBuilder in 4.0 is not the change from an unsafe string to char[] - it's the fact that StringBuilder is now actually a linked-list of StringBuilder instances.


The reason for this change should be obvious: now there is never a need to reallocate the buffer (an expensive operation, since, along with allocating more memory, you also have to copy all the contents from the old buffer to the new one).

This means calling ToString() is now slightly slower, since the final string needs to be computed, but doing a large number of Append() operations is now significantly faster. This fits in with the typical use-case for StringBuilder: a lot of calls to Append(), followed by a single call to ToString().


You can find benchmarks here. The conclusion? The new linked-list StringBuilder uses marginally more memory, but is significantly faster for the typical use-case.

like image 21
BlueRaja - Danny Pflughoeft Avatar answered Sep 22 '22 01:09

BlueRaja - Danny Pflughoeft