Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String object

 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice touch Jose, I think you're right...
"Hello", "Pal" and "HelloPal" will be created and interned !
Val
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic