File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

do StringBuffer have String pool

 
geeta vemula
Ranch Hand
Posts: 208
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic