• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Object creation

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Morrow:
As a very mild nitpick



Indeed.

 
Ranch Hand
Posts: 2108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone, I m clear to it now.................
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic