• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

String objects creation

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,
I've found this question one of our mock exam paper when i see this ,i just stuk up how it'll be the answer please provide me any insights on this.
How many String objects are created when we run the following code.
String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;
A.1
B.2
C.3
D.4
E.We can't say.
Ans:- c
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guru,

Given>

1.s1 = "Hello"; // Object n pool
2.s2 = s1;
3.s3 = s2 + "Pal";
// since reference var involvs in concat opr,new String created.
[
which implies one in heap, one in pool(since "HelloPal" doesn't exist earlier) will be created
].
4.s4 = s3;

Hope its clear.
 
guru sudhakar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi srinivasan,

Thanks for your reply.

how many string objects created in this scenario. s1,s2,s3,s4 total 4 right. what could be the eleminated one bcoz ans is 3 right.

please respond.

Thanks
Guru
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;
A. 1
B. 2
C. 3
D. 4
E. We can't say.
Ans:- c



Hi Guru,

"Hello" and "Pal" are string objects which are created in pool. s1 and s2 point to the same "Hello" object in pool whereas "HelloPal" is another object which is created during concatenation pointed by s3 and s4. Therefore total object in a pool is 3.

Remember whenever you use a different string in your program e.g. "pal" a pool object is created.

Regards
Gaurav
SCJP 1.5
 
guru sudhakar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot gaurav.

Regards
Guru
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic