You are on page 1of 4

7/15/13

Does new operator create new StringBuffer object??? (SCJP forum at JavaRanch)

File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

Does new operator create new StringBuffer object???


posted 5/5/2005 3:22 PM

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......??

www.coderanch.com/t/249006/java-programmer-SCJP/certification/operator-create-StringBuffer-object

1/4

7/15/13

Does new operator create new StringBuffer object??? (SCJP forum at JavaRanch)

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

posted 5/5/2005 3:50 PM

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

SC JP 1.4, SC BC D 1.3 Sajid Moinuddin Ranch Hand Joined: Mar 19, 2005 Posts: 85

posted 5/5/2005 3:51 PM

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

posted 5/6/2005 7:46 PM

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

posted 5/6/2005 7:49 PM

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".

www.coderanch.com/t/249006/java-programmer-SCJP/certification/operator-create-StringBuffer-object

2/4

7/15/13

Does new operator create new StringBuffer object??? (SCJP forum at JavaRanch)

James C arman, President<br />C arman C onsulting, Inc. levani dvalishvili Ranch Hand Joined: Mar 01, 2005 Posts: 99

posted 5/6/2005 11:15 PM

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.

SC JP 1.5(Done) SC JA 1.0(Done)<br />SC WC D(in Progress...)

James Carman Ranch Hand Joined: Feb 20, 2001 Posts: 580

posted 5/6/2005 11:16 PM

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

posted 5/6/2005 11:23 PM

oops, I shoul dhave looked at them closer

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: Does new operator create new StringBuffer object???

www.coderanch.com/t/249006/java-programmer-SCJP/certification/operator-create-StringBuffer-object

3/4

7/15/13

Does new operator create new StringBuffer object??? (SCJP forum at JavaRanch)

Similar Threads RHE question - String buffer Object creation?? StringBuffer Question. number of objects created how many objects are created?
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 15, 2013 07:39:02 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/249006/java-programmer-SCJP/certification/operator-create-StringBuffer-object

4/4