• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

now.....its GC time

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its' GC now.C the following q.
Which is the earliest line in the following code after which the object created on the // 1 can be garbage collected, assuming no compiler optimizations are done?
public class NewClass
{
static String enterTheDragon()
{
String a = new String("hello");
String b = new String("world !"); // 1
String c = new String(a + b + ""); // 2
String d = b; // 3
b = a; // 4
d = a; // 5
return c; // 6
}
public static void main(String args[])
{
String s = enterTheDragon(); // 7
System.out.println(s); // 8
}
}
// Answer: line 5
But I think it is Line no: 4 .Ok..ok..I am telling wht. I think.
The string created in //1 i.e. "world !" which is ref. by var. b upto line no. 3 BUT after line no. 3 that means in line no. 4 , b gets a's value.So, now the variable (ref.) b is pointing to the string "hello" ...........that means the string created in line 1(i.e. "world !") is now hanging and eligible for GC.tay is why I am telling the ans. is libe 4.Thats all.
wht. u say ? am I going wrong somewhere?
<marquee> Ratul Banerjee </marquee>
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ratul,
You've overlooked line 3 <code>String d = b; </code>. 'd' points to the object "world!" until it is re-assigned to 'a' in line 5. So the correct answer is 'after line 5'
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited April 15, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic