Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object creation?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object creation??" Watch "Object creation??" New topic
Author

Object creation??

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
How many objects are created by the following code and explain
why?
1.StringBuffer s1= new StringBuffer("abc");
2.StringBuffer s2= s1;
3.StringBuffer s3= new StringBuffer("abc");
//Is it 2??


Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
Jayant Kulkarni
Ranch Hand

Joined: Dec 02, 2003
Posts: 56
Hi,
Yes it will create only 2 objects, since you specify two times the "new" operator.
s2=s1 will assign the previous reference and will not create any new object.
Thanks,
Jayanta
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
I would count the new's, so on face value it would seem like two, except if you are counting the string literal, which technically I guess would be number 3.

JLS: Each string literal is a reference (�4.3) to an instance (�4.3.1, �12.5) of class String (�4.3.3). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (�15.28)-are "interned" so as to share unique instances, using the method String.intern.


<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
hi Jim,
where did you get this kind of question.When i saw this question , suddenly i thought 2 , but actually 3. Very good tempting question.


Regards,<br />Yasin<br />SCJP,SCWCD,SCBCD
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Originally posted by Mohamed Yasin:
hi Jim,
where did you get this kind of question.When i saw this question , suddenly i thought 2 , but actually 3. Very good tempting question.

It wasn't my question, I just attempted to answer it.
Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
Hi,
I found this question on the CD that comes with the java certification book by Simons.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Object creation??
 
Similar Threads
How many Objects are created ?
How many objects created?
StringBuffer Question.
number of objects created
how many objects are created?