wood burning stoves 2.0*
The moose likes Java in General and the fly likes String Builders Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "String Builders" Watch "String Builders" New topic
Author

String Builders

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 217

Hi all,
I have two questions to ask-
1.In a StringBuilder class,when we create it's object,we pass a string literal as parameter.Does this literal also goes in the constant pool?
2.During chaining of methods in case of Strings,such as "x.concat("def").toUpperCase().repalce('x',"X")",if the temporary objects created are lost,how come methods are invoked on them when they are lost.i.e if x.concat("def") creates a object which is lost,how come toUpperCase() works?

Please pardon me if i have asked a silly question,and be patient to look at my problem.

Thanks...
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

1. The String literal goes in the pool when the class is loaded, along with any and all other String literals in the class. That the String literal is used as the argument to a StringBuilder constructor is irrelevant.

2. You need to explain what you mean by 'lost'. The chained method is invoked on the returned object from the preceding method. After execution, there is no longer a reference to the intermediate values, so they are eligible for GC.


luck, db
There are no new questions, but there may be new answers.
Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 217

Thanks for the quick reply,
Actually what i am concerned about is the number of objects created.
StringBuilder build=new StringBuilder("abc");

Now,how many objetcs?
And one more thing,if again "abc "is created in the string constant pool,and i use several such operations,won't it again cause a memory wastage due to excessive string literals in string constant pool?

Thanks...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

Sudhanshu Mishra wrote:StringBuilder build=new StringBuilder("abc");

Now,how many objetcs?

Two; one String object and one StringBuilder object.

Sudhanshu Mishra wrote:And one more thing,if again "abc "is created in the string constant pool,and i use several such operations,won't it again cause a memory wastage due to excessive string literals in string constant pool?

The whole point of the string pool is to reuse String objects. If you use the same string literal multiple times in your source code, then they will all refer to the same String object that's in the string pool. For example:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40057
    
  28
Search for "Strings, literally" and you should find an old JavaRanch article of that name which tells you all about how Strings behave.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Builders