Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock exam question

 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This question is from the website :http://valiveru.tripod.com/java/jvaltest.html
Question 40.
When is the string object created in line2 first subjected to Garbage Collection(Assuming amethod is part of valid java class)
1public void aMethod(){
2 String s1 = "Hello";
3 String s2 = "pal";
4 s1 = s1 + s2;
5 System.out.println(s1);
6}

A.After the execution of method, i.e., after line 6.
B.After line 4.
C.After line 3.
D.After line 2.

Anser is B
Can someone explain why, I think the answer is D
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is after line 4.
Because after line 4 the string hello that was created in line 2 is orphan and cannot be referenced in any way .
And moreover on other string is referring to a string hello also.

If it is after line 2 the nat line 3 we can not refer to hello which is wrong .
That is why the answer is afte line 4.
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sdev,
i really don't think that this is a valid question because none of the answer seems correct to me.
String object created in 'Sring pool' are not subject to be GC. u can go thru the following link http://www.javaranch.com/ubb/Forum24/HTML/000126.html
hope this will help u.
Looking for some help from java expert.
regards
vivek
[This message has been edited by Vivek Shrivastava (edited July 18, 2000).]
 
John Fairbairn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading the previous discussions about string eligibility for gc, I agree with Vivek. String literals are not available for gc only string objects. Looking at declarations below:
1 public void aMethod(){
2 String s1 = "Hello"; //s1 is a string literal
3 String s2 = "pal"; //s2 is a string literal
4 s1 = s1 + s2; //s1 is still a string literal
5 System.out.println(s1);
6 }
Again, the previous discussion board supplied by Vivek explains this in detail.
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

After reading the previous discussions about string eligibility for gc, I agree with Vivek. String literals are not available for gc only string objects. Looking at declarations below:
1 public void aMethod(){
2 String s1 = "Hello"; //s1 is a string literal
3 String s2 = "pal"; //s2 is a string literal
4 s1 = s1 + s2; //s1 is still a string literal
5 System.out.println(s1);
6 }
Again, the previous discussion board supplied by Vivek explains this in detail.

Hi John,
I would like to say that 's1' refrence at line# 4 is not a refrence to a String literal in 'String pool'. it is now a refrnece to a String Object on heap. ( but the String literal created at line# 2 is still in 'String pool' and is not available for gc.)
If i change your program little bit like:
1 public void aMethod(){
2 String s1 = "Hello"; //s1 is a string literal
3 String s2 = "pal"; //s2 is a string literal
4 s1 = s1 + s2; //s1 is still a string literal
5 System.out.println(s1);
6 s1=null;
7 }
Now 's1' is available for gc after the line# 6.

I hope u would be agree with me.
please feel free to correct me.
regrads
vivek
 
John Fairbairn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right my friend. At Line #4, s1 is a reference to a String object on the heap so it would be available for gc if set to null.
But I am confused as to why:
s1 = s1 + s2 \\s1 is a string object reference
and
s1 = "Hello" + "pal" \\s1 is a string literal
I read the previous discussion but am still bothered.
Doesn't the use of use of the '+' operator with strings always return a new string object?
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi John,
In the link I had suggested jim has provided a link to JLS (here). So if u go there and read u will find following statement.

Strings computed by constant expression (�15.27) are computed at compile time and then treated as if they were literal.


If we go through the example provided by jim
String a = "Bill"; // String constant
String b = "Gates"; // String constant
String b = "Bill" + "Gates"; // String constant
String d = a + b; // not a String constant
See last statement is not a String literal because it is not a String constant expression.Result is not known at compile time because it is based on variables.
However if we change the example little bit and make both the variables as final variable
final String a = "Bill"; // String constant
final String b = "Gates"; // String constant
String d = a + b; // this *is* a String constant
See Now last statement is a String literal because now it is a String constant expression. Result is known at compile time because it is based on final variables.
Hope this will help u. feel free to correct me any time.
Regards
Vivek

PS: jim I am using your example to explain the things with little change. Please correct me if I am wrong.


[This message has been edited by Vivek Shrivastava (edited July 18, 2000).]
 
John Fairbairn
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhhh... i see the light finally. Thank you very much Vivek.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic