| Author |
Does new operator create new StringBuffer object???
|
Amit Das
Ranch Hand
Joined: Mar 05, 2005
Posts: 206
|
|
hi all, have a doubt.... StringBuffer s1 = new StringBuffer("abc"); 2. StringBuffer s2 = s1; 3. StringBuffer s3 = new StringBuffer("abc"); How many objects are created ? ans given is 3......(How???) i think it shud be 2.... is the proceeding stmt true......??
StringBuffer type reference variable will not point to a new object, but to same pool object which was created when class was loaded....
plz reply, thanx amit
|
 |
Soni Prasad
Ranch Hand
Joined: Mar 09, 2005
Posts: 97
|
|
3 is the right answer 1st string object "abc" in the pool 2nd StringBuffer object due to new for s1 3rd StringBuffer object due to new for s3 new operator always creates a new object on the heap
|
SCJP 1.4, SCBCD 1.3
|
 |
Sajid Moinuddin
Ranch Hand
Joined: Mar 19, 2005
Posts: 85
|
|
i think the other object created is a String object "abc". if u had putten line three as below 3. StringBuffer s3 = new StringBuffer("abcd"); number of object created would have been 4. i am not sure though. "abc" sould be kept into the string pool. correct me if i am wrong sajid
|
 |
Pally Gharmount
Ranch Hand
Joined: Aug 25, 2004
Posts: 34
|
|
i am very sure the answer is still 2, as there are only 2 StringBuffer objects being created with the new keyword but one reference (s2) points to an existing object (s1). [ May 06, 2005: Message edited by: Pally Gharmount ]
|
 |
James Carman
Ranch Hand
Joined: Feb 20, 2001
Posts: 580
|
|
|
The String you're using to initialize your two StringBuffers has to be created. So, there are 3 objects, 2 StringBuffers, and 1 String, as others have pointed out. The reason that it's not 4 is because the String literal is reused; "abc" == "abc".
|
James Carman, President<br />Carman Consulting, Inc.
|
 |
levani dvalishvili
Ranch Hand
Joined: Mar 01, 2005
Posts: 99
|
|
as far as I know if you explicitly giving a new operator to a String it makes sure that that string does not end up in a pool of strings, which is used for identical string reuse, so basicly as one of the books said, if you explicitly create a string with new operator , it does not go into pool of string so on the second line string reuse does not hapen that is why answer is 3.
|
SCJP 1.5(Done) SCJA 1.0(Done)<br />SCWCD(in Progress...)
|
 |
James Carman
Ranch Hand
Joined: Feb 20, 2001
Posts: 580
|
|
Originally posted by levani dvalishvili: as far as I know if you explicitly giving a new operator to a String it makes sure that that string does not end up in a pool of strings, which is used for identical string reuse, so basicly as one of the books said, if you explicitly create a string with new operator , it does not go into pool of string so on the second line string reuse does not hapen that is why answer is 3.
They are not Strings, but StringBuffers. The "pool" concept doesn't come into play at all.
|
 |
levani dvalishvili
Ranch Hand
Joined: Mar 01, 2005
Posts: 99
|
|
oops, I shoul dhave looked at them closer
|
 |
 |
|
|
subject: Does new operator create new StringBuffer object???
|
|
|