• 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

do StringBuffer have String pool

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all

StringBuffer s1="ABC";
StringBuffer s2="ABC";
StringBuffer s3=s1;

Can StringBuffer have Strings pool. In this how many objects are created?

Thanks,
Geeta Vemula.
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We cannot create StringBuffer objects with the syntax you specified.

Moreover A string buffer implements a mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
source


You may read the description given in the API. They have clearly stated that their value can be changed so using string pool will be worthless.
[ December 04, 2008: Message edited by: Himanshu Gupta ]
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you modify a StringBuffer, the same object is modified. No other objects are created.
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this from Complete Java 2 Certification guide

How many objects are created in the following?

StringBuffer s1=new StringBuffer("abc");
StringBuffer s2=s1;
StringBuffer s3=new StringBuffer("abc");

Answer is 3.
explanation is : two StringBuffer objects at run time and one at comile time i.e "abc" in the string pool.
So does it mean whatever classes or constructors takes strings as arguments will create one additional object?
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IN that sense the answer is right. The point is whenever we make a string like "abc" one objet is formed in the string pool. SO when you write



two objects are formed. One is of StringBuffer and other is String Object(abc) itself.

When you make another StringBuffer Object passing abc as its parameter then


one new Object of StringBuffer is formed and the String Object(abc) will be used from the string pool.

So in total 3 objects will be formed.

We had a similar discussion in which one rancher asked that which is the better way to make a String object out of the given two?



The answer is the first one as it creates only one Object while the other one creates two Objects. One Object gets created when you write "abc" and another when you assign it to the reference.


Hope it makes you understand better.
[ December 04, 2008: Message edited by: Himanshu Gupta ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic