• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Builders

 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Search for "Strings, literally" and you should find an old JavaRanch article of that name which tells you all about how Strings behave.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic