Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does new operator create new StringBuffer object???

 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sajid Moinuddin
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
levani dvalishvili
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops, I shoul dhave looked at them closer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic