pagano monello wrote:I have read that it is possible to define an initial length for a variable of this class, as it follows:
You don't provide an initial length, but an initial capacity. These are 2 different things! More detailed info can be found here and here.
pagano monello wrote:I find also strange that it's not allowed to assign a value to a StringBuilder variable, like this:
Nothing strange at all! String and StringBuilder are 2 different classes, so you can't assign the one to the other directly. It's like assigning a Cat instance to a Dog reference variable, it's also not allowed and won't compile!
But you can create a StringBuilder from a String using one of the overloaded constructors:
And you can assign the contents of a StringBuilder to a String using its toString method: