aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String object" Watch "String object" New topic
Author

String object

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Hi all,
I take this from Valiveru's Home
Question 45.
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.
And the answer is C. Can you help me about it?
thanks
daniel
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Fisher,
The answer given is wrong! The correct answer should be B 2 new objects are created. I give you the following code as proof.

Regards,
Manfred.
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
Yeap..the answer is B. Here are some points to keep in mind.
1.when you do String s = "Hello"; //one object is created
2.when you do String s2 = s; //no object is created because the object from the string pool is referenced by s2
3.when you do String s3 = s+"Pal"; //one object is created because this is a new string (HelloPal).
4.Again when you do String s4 = s3; //no object is created because the object from the string pool is referenced.
I hope this explanation helps..
Roopa
Originally posted by Fisher Daniel:
Hi all,
I take this from Valiveru's Home
Question 45.
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.
And the answer is C. Can you help me about it?
thanks
daniel

Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I still think the answer is three.
The question is how many objects are created, not how many objects are created and assigned to a variable.
"s3 = s2 + "Pal";"
here I think the JVM creates and intern a String object for Pal literal as previously did for Hello.
The code only compares variables. The Pal string resides in the String pool without any other reference pointing it.


SCJP2. Please Indent your code using UBB Code
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Nice touch Jose, I think you're right...
"Hello", "Pal" and "HelloPal" will be created and interned !
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Maybe this is starting to get a bit too theoretical, but since it doesn't actually matter how many strings are created, can't an optimizer interfere?
What if
String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;
was changed at compile time to
String s1,s2,s3,s4;
s1 = "Hello";
s2 = "Hello";
s3 = "HelloPal";
s4 = "HelloPal";
I don't know any rule that would prevent this. Do you?
Raj Shah
Greenhorn

Joined: Sep 24, 2001
Posts: 14
Hi there, while you all are at String literals, could somebody let me know for sure whether the String literals in the pool maintained by the class are eligible for garbage collection or not? assuming that there is no other reference existing. I read in one of the sessions here that they are Neither eligible for gc Nor garbage collected in any circumstances till the class remains loaded in the jvm. But the Exam questions on no. of objects eligible for gc normally counts such literals as well. Please answer. Thanks, Raj Shah
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Which class needs to be downloaded for the interned strings being g.c.ed? if we knew we could give it a try.
This implies that the string pool is asociated with a specific class?
Maybe the string pool is referred to for the whole duration of the program.
I have no read the whole JLS yet but I don't think the real exam ask questions about g.c.ing string literals.
 
Don't get me started about those stupid light bulbs.
 
subject: String object