Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

GC...

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all...
When will be the first time an object will be eligible for GC in below prog.
1) String x = new String("Hello");
2) String y = x+ "World";
3) x = null;
4) System.out.println("Test ");
5) y = "Java";
Thanks
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After
x = null;
since the garbage collector tries to locate objects that no longer have references.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. When the object no longer has "any" references, it will be can be garbage collected. But it does not mean it definitely will be.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Taking the similar code.
At line 5 instead of y="Java" if we replace by x="Java", does this mean that x will be Garbage collected in line 3 and that we can no longer assign a value to x. I have in mind the fact that x just becomes eligible for garbage collection, but does not necessarily get collected. Can we assign a value?
Shiraz.

Originally posted by Shah Chunky:
Hello all...
When will be the first time an object will be eligible for GC in below prog.
1) String x = new String("Hello");
2) String y = x+ "World";
3) x = null;
4) System.out.println("Test ");
5) y = "Java";
Thanks


 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shiraz and Chunky,


Originally posted by Shah Chunky:
Hello all...
When will be the first time an object will be eligible for GC in below
prog.
<code>
1) String x = new String("Hello");
2) String y = x+ "World";
3) x = null;
4) System.out.println("Test ");
5) y = "Java";
</code>


Well, when you create an Object by a call to <code> new </code> operator, the <code>new </code> returns a reference
to the object being created. In querry posted by Chunky the
object that is created is a string object "Hellow" which is referenced by the variable <code> x </code>. Strings are immutable so on line 2 we have another string object by
<code>Hellow World </code> which is referenced by the <code> y
</code>
variable. At line 3 the object of String type "Hellow" is no longer is refernced by any variable so it becomes eligible for garbage collection. Please remember it is not "x" that is garbage collected but rather string available in the pool "Hellow" that is garbage collected.
Hope this clarifies this issue Kindly revert back incase you have
further doubts.
Regards,
Ravindra Mohan
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic