Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object creation

 
sachin pachpute
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Steve Sugden
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Morrow:
As a very mild nitpick


Indeed.

 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am i wrong?

Nope. There are two String objects referenced by that code. Good job!
 
sachin pachpute
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone, I m clear to it now.................
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic