aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 207

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: 4522
    
    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: 207

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: 13868
    
  10

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 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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
 
Similar Threads
Doubt in K&B SCJP 5: topic Important Facts About Strings and Memory
Objects eligible for GC
StringBuffer and Memory
String concat
String objects are immutable they can be shared. I do no get what this means...please help.