Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class StringBuilder - A pair of issues

 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have read that it is possible to define an initial length for a variable of this class, as it follows:

however, if i try to print the length of the variable, as follows:

I get zero, instead of 50. Since I expected 50, can anyone tell me why I am wrong?

I find also strange that it's not allowed to assign a value to a StringBuilder variable, like this:

I guess I have to read the class documentation to understand why this is not possible, but I try to ask you here anyway, maybe someone can give me a simple fast tip about it.

Thank you in advance.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

Hope it helps!
Kind regards,
Roel
 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really clear and helpful.

Thank you Roel then!

Bye
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic