my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String objects creation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "String objects creation" Watch "String objects creation" New topic
Author

String objects creation

guru sudhakar
Greenhorn

Joined: Apr 02, 2007
Posts: 8
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
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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.


Thanks & Regards,<br />T.Srinivasan,<br />SCWCD 1.4(89%),SCJP 5.0(75%)<br />"That service is the noblest which is rendered for its own sake." - Mahatma Gandhi
guru sudhakar
Greenhorn

Joined: Apr 02, 2007
Posts: 8
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
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
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

Joined: Apr 02, 2007
Posts: 8
Thanks a lot gaurav.

Regards
Guru
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String objects creation
 
Similar Threads
String Objects
please help me
String Objects
question...
Mock Exam Q