• Post Reply Bookmark Topic Watch Topic
  • New Topic

What will happen when create a StringBuilder?  RSS feed

 
Nicola Viola
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Team,
I would like to know what happens when I write that:



I am pretty sure I will create a StringBuilder object into the heap, but will I put the litteral "ciao" into the String Constant Pool too? and is it an Object?
By the way, how many objects will I create running this command?

Regards
Nicola
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do YOU think might happen, and why?
 
Nicola Viola
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Fred,

thank you for the quick response.

I think this command creates a StringBuilder object into the heap and "ciao" string into the String Constant Pool.

Although StringBuilder hands the "ciao" String, "ciao" is a literal so it should be into the pool.

I think if I write a literal string it will stay into the pool.

waiting for a your response, I give you my regards.

Thank you

Nicola
 
Tim Holloway
Saloon Keeper
Posts: 18797
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More or less. In addition, the object reference "sb" will be allocated on the current method's stack.

"ciao" is a string literal, which is a java.lang.String object. Java's "interning" mechanism may attempt to locate identical occurrences and fold them down into a single instance.

So. What do you think will happen when you invoke the "toString()" method on sb?
 
Nicola Viola
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Tim for your help,

maybe "toString()" will create a string's copy. In that way you will recive the bit pattern of the new String and you will be able to modify it without change the StringBuilder value.

I'm waiting to know if my answer could be ok.

Regards

Nicola
 
Tim Holloway
Saloon Keeper
Posts: 18797
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah. But you should read the JavaDocs!

Sun JavaDocs wrote:
A new String object is allocated and initialized to contain the character sequence currently represented by this object. This String is then returned. Subsequent changes to this sequence do not affect the contents of the String.
 
Nicola Viola
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Tim,
thank you for your reply.

I looked at the toString method's source code and it reports:



Where is my mistake?
I am looking forward to hearing you again.

Nicola
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check the source code of java.lang.String as well, and check what the String(StringBuilder) constructor does?
 
Nicola Viola
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
thank you for your answer, but why should I check how the String(StringBuilder) works?



I would only like to know if

creates a String into the pool.

Thank you

Nicola
 
Tim Holloway
Saloon Keeper
Posts: 18797
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stringbuilder builds no strings, pool or otherwise. It's simply a buffer object that is used to assemble sequences of characters.

The String(StringBuilder) constructor builds a new string, using the current StringBuilder buffer contents.

Note that a lot of object/string conversions can be done in 2 ways:

1. myObject.toString();

2. new String(myObject);

Usually - as in the case of StringBuilder, the "toString" method will invoke the corresponding String constructor. This keeps things consistent regardless of which method you use and defined in one place. In cases where the object class isn't one of the core supported object classes, however, alternative string conversion methods may be used.
 
Nicola Viola
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you indeed to everyone!

Best regards

Nicola
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't find the StringBuilder#getValue method in the documentation; I presume it has package‑private access. It is maybe unfortunate that there isn't a String#valueOf(StringBulider) method.
 
Tim Holloway
Saloon Keeper
Posts: 18797
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. it's an internal method.

There is a String.valueOf(StringBuilder) method: it's the String(StringBuilder) constructor.

If you'll note, all but one of the String.valueOf() methods takes an argument that's a primitive. That's because something like "1234.toString()" isn't - or at least wasn't a valid construct. Auto-boxing may have changed that in more recent language releases.

The one exception for valueOf is the String.valueOf(Object), method. Which returns either a simple object.toString() or the string "null", if the object is null. In fact, that last is probably what mandated the need for valueOf() for class Object.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point; you can use that valueOf method with any Object.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!