Skip to main content
added 498 characters in body
Source Link
Tim Bender
  • 20.5k
  • 2
  • 52
  • 59

The only thing I can think of is that no memory was allocated, but then what's the point of StringBuilder(int capacity)?

The point is to provide an up front opportunity to allocate a sufficiently large chunk of memory to handle all the data that will be inserted. When the capacity is exceeded, internally a new chunk of memory will have to be allocated and all the values from the old memory copied over. As you can imagine, this could be a performance burden if it occurs a multitude of times.

The behavior is not really odd or unexpected, in fact it is quite consistent with the practice of encapsulation.

What would ArrayList do? An ArrayList is backed by an array, and using a sufficient initial capacity is more performant, however doing so still does not allow arbitrary access to indices of the array.

I think the odd behavior is thinking of a StringBuilder as a representation of a char[]. The representation is properly encapsulated at it is, don't poke at it in curious ways.

The behavior is not really odd or unexpected, in fact it is quite consistent with the practice of encapsulation.

What would ArrayList do? An ArrayList is backed by an array, and using a sufficient initial capacity is more performant, however doing so still does not allow arbitrary access to indices of the array.

I think the odd behavior is thinking of a StringBuilder as a representation of a char[]. The representation is properly encapsulated at it is, don't poke at it in curious ways.

The only thing I can think of is that no memory was allocated, but then what's the point of StringBuilder(int capacity)?

The point is to provide an up front opportunity to allocate a sufficiently large chunk of memory to handle all the data that will be inserted. When the capacity is exceeded, internally a new chunk of memory will have to be allocated and all the values from the old memory copied over. As you can imagine, this could be a performance burden if it occurs a multitude of times.

The behavior is not really odd or unexpected, in fact it is quite consistent with the practice of encapsulation.

What would ArrayList do? An ArrayList is backed by an array, and using a sufficient initial capacity is more performant, however doing so still does not allow arbitrary access to indices of the array.

I think the odd behavior is thinking of a StringBuilder as a representation of a char[]. The representation is properly encapsulated at it is, don't poke at it in curious ways.

Source Link
Tim Bender
  • 20.5k
  • 2
  • 52
  • 59

The behavior is not really odd or unexpected, in fact it is quite consistent with the practice of encapsulation.

What would ArrayList do? An ArrayList is backed by an array, and using a sufficient initial capacity is more performant, however doing so still does not allow arbitrary access to indices of the array.

I think the odd behavior is thinking of a StringBuilder as a representation of a char[]. The representation is properly encapsulated at it is, don't poke at it in curious ways.