File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object creation" Watch "Object creation" New topic
Author

Object creation

sachin pachpute
Ranch Hand

Joined: Nov 04, 2004
Posts: 38
Can anybody explain me how there are 4 objects created? I know 3.


3. Given the following,
13. String x = new String("xyz");
14. y = "abc";
15. x = x + y;
how many String objects have been created?
A. 2
B. 3
C. 4
D. 5


Ans--C. Line 13 creates two, one referred to by x and the lost String “xyz”. Line 14 creates one
(for a total of three). Line 15 creates one more (for a total of four), the concatenated String
referred to by x with a value of “xyzabc”.
A, B, and D are incorrect based on the logic described above.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Have you read the explanation? It explains exactly how 4 strings are created. What part do you not understand?

This question was also asked in the last few days. Have you looked through the previous recent threads?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Steve Sugden
Greenhorn

Joined: Aug 20, 2005
Posts: 8
Perhaps its the creating of *two* strings on line 13? One is the literal "xyz" (created in a similar way to "abc" on the next line) and the other is created when the "new" operator is invoked. x refers to the "new" String; as stated the "xyz" literal is "lost".

Does this help?
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

x refers to the "new" String; as stated the "xyz" literal is "lost".
As a very mild nitpick, the String object pertaining to the "xyz" literal isn't exactly "lost". It remains on the heap, accessible via another literal or via the intern() method.
Steve Sugden
Greenhorn

Joined: Aug 20, 2005
Posts: 8
Originally posted by Steve Morrow:
As a very mild nitpick


Indeed.

Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
sachin, another exercise for you on this issue

x="sss";
y="sss";
z=new String("sss");
a=x;
b=z;

this is a good one.

how many are created?
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Originally posted by Jesus Angeles:
sachin, another exercise for you on this issue

x="sss";
y="sss";
z=new String("sss");
a=x;
b=z;

this is a good one.

how many are created?


well 2 are created.
one "sss"
Second for new String(). In the pool "sss" is not created when new String("sss") is done since it already exists?

Am i wrong?


/** Code speaks louder than words */
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Am i wrong?

Nope. There are two String objects referenced by that code. Good job!
sachin pachpute
Ranch Hand

Joined: Nov 04, 2004
Posts: 38
Thanks everyone, I m clear to it now.................
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object creation