| Author |
do StringBuffer have String pool
|
geeta vemula
Ranch Hand
Joined: Jul 18, 2008
Posts: 208
|
|
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
Joined: Aug 18, 2008
Posts: 598
|
|
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 ]
|
My Blog SCJP 5 SCWCD 5
|
 |
Krishna Srinivasan
Ranch Hand
Joined: Jul 28, 2003
Posts: 1803
|
|
|
When you modify a StringBuffer, the same object is modified. No other objects are created.
|
Krishna Srinivasan
OCAJP Mock Questions
|
 |
geeta vemula
Ranch Hand
Joined: Jul 18, 2008
Posts: 208
|
|
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
Joined: Aug 18, 2008
Posts: 598
|
|
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 ]
|
 |
 |
|
|
subject: do StringBuffer have String pool
|
|
|