• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Garbage Collector

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
if I change the line 3 with this line: String s2 = "hello"+"world";
the answer of this question below would be de same, or not? I think it would be changed, because now I deal with a String in a String Pool, so it will never be eligible to be collected.

QUESTION
1. String s = "abcd";
2. Integer x = new Integer(3);
3. String s2 = s+4;
4. s2=null;
5. s=null;

Following the execution of which line above,
is the object referenced by s2 available for
garbage collection?
ANSWER: 4
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello igor

no the answer should not change

krish bajaj
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Th eanswer will not change since "S2 = null" makes it available for GC what ever may be the value that you assigned to S2 it does'nt matter.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once u make a reference variable equal to

null

, that means that variable is not refering to anything. And the object that was been refered to, by the reference variable will now be lost [unless it has been referenced by another reference variable]. This lost object now becomes eligible for GC
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its posted somewhere that real exam would not contain questions regarding GC in literal pool.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if I change the line 3 with this line: String s2 = "hello"+"world";
the answer of this question below would be de same, or not? I think it would be changed, because now I deal with a String in a String Pool, so it will never be eligible to be collected.


You are right.
 
reply
    Bookmark Topic Watch Topic
  • New Topic